开发者

compare a postcode string to a column of 3-4 character values

I am trying to get a user to enter a postcode and then compare the first few characters to see if they exists in a column in a database.

Basically the postcode entered for example could be bt1 5ws. I开发者_StackOverflow中文版n the column in the sql server database there are beginning of postcodes for example bt1, bt10, iv1.

The idea is to find out what area the user is from.

var Pcode = storeDB.DeliveryPCodes.SingleOrDefault(x => x.PCode == Customer.PCode)).ToString();

the above code compares the whole postcode entered to the with the code in column and does not work.

i am using c#, mvc3, entity framework (code first) with sql server 2008.


You can use string methods like StartsWith

var code = Customer.PCode.SubString(0, 3);

var Pcode = storeDB.DeliveryPCodes.
   SingleOrDefault(x => x.PCode.StartsWith(code)).ToString();


Try this:

string postCode = Customer.PCode;
var PCode = storeDB.DeliveryPCodes.FirstOrDefault(x => postcode.StartsWith(x.PCode))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜