entity framework – Linq to EF Search for a string that does not start with a Letter – Education Career Blog

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, e.g. “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.Name0)
              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.Name0)

To

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

Leave a Comment