Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Ideas?

like image 530
jamiegs Avatar asked Aug 19 '11 16:08

jamiegs


People also ask

How do I get a single column value in Entity Framework?

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.

What is Entitystate in Entity Framework?

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.

How do I get specific columns in Entity Framework?

In Entity Framework Core we can select specific columns by using either anonymous types or DTOs.

What is column order in entity Framework?

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.


1 Answers

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.

like image 97
Jethro Avatar answered Sep 19 '22 00:09

Jethro