开发者

Entity Framework: where field starts with number

I'm using MVC3 with Entity Framework 4.1 and I've got a grid that is paged by the first character of the company name. I have it working great but some companies start with a number. I'm using the following LINQ query to get the companies starting with the selected number, but how can I select those starting with a number?

    var b = (from c in dbContext.Companies
             where c.CompanyName.StartsWith(selectedCharacter)
             select c)

I've tried:

 where char.IsNumber(l.CompanyName[0]) 

But, I get an error because it doesn't know how to convert it to SQL.

Edit: I know I could just do .CompanyName.StartsWith("1") || .CompanyName.StartsWith("2"), etc.. Is there a better way? . Any I开发者_如何学运维deas?


You could do something like this.

var numbers = new string[]{"1","2","3","4","5","6","7","8","9","0"};
var b = (from c in dbContext.Companies
         where numbers.Contains(c.CompanyName.Substring(0,1))
         select c).ToList();

You could run into an issue though if your company is Empty.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜