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

2
  • 4
    Because it couldn't be translated to T-SQL, Linq to Entities couldn't recognize it. Read more stackoverflow.com/a/34061692/2946329 Commented Dec 20, 2016 at 10:23
  • I understand from Thomas' answer that s.Surname.ToArray()[0] is a char. The Linq namespace contains an extension on IEnumerables Contains(char); is that usable? Alternatively, could you use IndexOf(char) and test for != -1? Commented Dec 20, 2016 at 10:31

2 Answers 2

8

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()));
Sign up to request clarification or add additional context in comments.

3 Comments

How can i match char and string?
@s.k.paul what do you mean?
initials is a List<string> whereas s.Surname.First() returns a char. How can I compare them?
2

The following code solved my problem-

 var initials = Queries["SurnameInitial"].Split(',').ToList();
 filter.And(s => initials.Contains(s.Surname.Substring(0, 1)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.