Reputation: 1791
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 Ideas?
Upvotes: 7
Views: 4227
Reputation: 5916
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.
Upvotes: 11