if (!string.IsNullOrEmpty(Queries["SurnameInitial"])) { var initials = Queries["SurnameInitial"].Split(',').ToList(); filter.And(s => initials.Contains(s.Surname.ToArray()[0].ToString())); }
它抛出异常
LINQ to Entities does not recognize the method ‘System.String ToString()’ method,and this method cannot be translated into a store expression.
我怎样才能匹配char vs string?
解决方法
您可以使用以下s.Surname.First()代替:
if (!string.IsNullOrEmpty(Queries["SurnameInitial"])) { var initials = Queries["SurnameInitial"].Split(','); filter.And(s => initials.Contains(s.Surname.First())); }
这是因为Linq to Entities不知道如何处理char.ToString()
由于您正在处理List< string>您可以使用:
filter.And(s => initials.Any(x => x.Contains(s.Surname.First()));