Entity Framework: where the field begins with a number

I am using MVC3 with Entity Framework 4.1 and I have a grid on which the first character of the company name is called. It works fine for me, but some companies start with a number. I use the following LINQ query to get companies starting with the selected number, but how can I select those starting with the number?

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

I tried:

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

But I get an error because he does not know how to convert it to SQL.

Edit: I know I can just do .CompanyName.StartsWith ("1") || .CompanyName.StartsWith ("2"), etc. Is there a better way? Any ideas?

+6
source share
1 answer

You can 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 may run into a problem if your company is empty.

+9
source

Source: https://habr.com/ru/post/895433/


All Articles