简体   繁体   中英

Linq to EF Search for a string that does not start with a Letter

I am using Linq to Entity Framework 4, what I'm trying to do is build a query that finds all supplier entities that do not start with a letter, typically these are numbers, eg "1st Choice" I thought this would be trivial and wrote this:

var letters = Enumerable.Range('A', 26).Select(x => (char)x);
var results = from supplier in All()
              where !letters.Contains(supplier.Name[0])
              select supplier;

return results.ToList();

Unfortunately this fails with the error:

System.NotSupportedException`
"Unable to create a constant value of type 'System.Char'. 
Only primitive types ('such as Int32, String, and Guid') are supported in this context."

Irritatingly this same query works fine in LinqPad as a Linq To Sql query. How can I get around this?

var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var results = from supplier in All()
              where !letters.Contains(supplier.Name.Substring(0, 1))
              select supplier;

Change

var letters = Enumerable.Range('A', 26).Select(x => (char)x);

To

var letters = Enumerable.Range('A', 26).Select(x => ((char)x).ToString());

And

where !letters.Contains(supplier.Name[0])

To

where !letters.Contains(supplier.Name.Substring(0, 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM