views:

26

answers:

2

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

+1  A: 

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))
Adam Robinson
+2  A: 
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var results = from supplier in All()
              where !letters.Contains(supplier.Name.Substring(0, 1))
              select supplier;
LukeH