if (!string.IsNullOrEmpty(Queries["SurnameInitial"]))
{
var initials = Queries["SurnameInitial"].Split(',').ToList();
filter.And(s => initials.Contains(s.Surname.ToArray()[0].ToString()));
}
It throws exception
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
How can i match char vs string?
You can use the following s.Surname.First()
instead:
if (!string.IsNullOrEmpty(Queries["SurnameInitial"]))
{
var initials = Queries["SurnameInitial"].Split(',');
filter.And(s => initials.Contains(s.Surname.First()));
}
This happens because Linq to Entities doesn't know what to do with char.ToString()
Since you're dealing with a List<string>
you can use:
filter.And(s => initials.Any(x => x.Contains(s.Surname.First()));
The following code solved my problem-
var initials = Queries["SurnameInitial"].Split(',').ToList();
filter.And(s => initials.Contains(s.Surname.Substring(0, 1)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With