I’m using Entity Framework and have come across an issue with the lack of support for certain extensions.
I have 2 keys (both char) fromKey and toKey and am looking to construct a Where statement that will search a list of Stores, and select where the first letter of Store.Name is between fromKey and toKey.
string[0] does not work, Substring does not work – anyone got any ideas how to get round this?
Code for substring method:
public static IQueryable<Store> FindActiveByName()
{
var r = new ReadRepo<Store>(Local.Items.Uow.Context);
Tuple<string, string> range = Tuple.Create("0", "9");
return r.Find().Where(s => range.Item1 <= s.Name.Substring(0, 1) &&
s.Name.Substring(0, 1) <= range.Item2);
}
Gives me an error:
Operator <= cannot be applied to string
UPDATE
Anwser provided by nemesV
public static IQueryable<Store> FindActiveByName()
{
Tuple<char, char> range = Tuple.Create('R', 'S');
var r = new ReadRepo<Store>(Local.Items.Uow.Context);
var keys = Enumerable
.Range(range.Item1, (int)range.Item2 - (int)range.Item1 + 1)
.Select(k => ((char)k).ToString());
return r.Find(s => keys.Contains(s.Name.Substring(0, 1)));
}
I don’t have EF at hand so maybe this is also not working, but you can try the following workaround:
First generate a string with the
fromtoletters. And then you can useContainsto check whether the first character of theNameis in the generated letters:Or you have the obviously working workaround:
.ToArray()before theWherewhich will force the query evaluation and theName.Substringcomparison will be done on client side.