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?
If you're fetching a single item only then, you need use select before your FirstOrDefault()/SingleOrDefault(). And you can use anonymous object of the required properties. var name = dbContext. MyTable.
EF API maintains the state of each entity during its lifetime. Each entity has a state based on the operation performed on it via the context class. The entity state represented by an enum System.
In Entity Framework Core we can select specific columns by using either anonymous types or DTOs.
Column OrderUse the zero-based Order parameter to set the order of columns in the database. As per the default convention, PK columns will come first and then the rest of the columns based on the order of their corresponding properties in an entity class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With