LINQ to Entities does not recognize the method Boolean ContainsAny

心已入冬 提交于 2019-12-11 16:29:24

问题


Error

LINQ to Entities does not recognize the method 'Boolean ContainsAny(System.String, System.String[])' method, and this method cannot be translated into a store expression.

Code

var predicate = FilterClients(clientRequest);
clients = db.Clients.Where(predicate).Include(x => x.Organization) 
.Include(x => x.Department).ToList();

private Expression<Func<Client, bool>> FilterClients(ClientRequest clientRequest)
{
   var predicate = PredicateBuilder.True<Client>();

   if (clientRequest.IsBySearchPatternFullName)
   {
      var searchPatternFullName = clientRequest.SearchPatternFullName.Trim();
      var matches = Regex.Matches(searchPatternFullName, @"\w+[^\s]*\w+|\w");
      var words = new List<string>();
      foreach (Match match in matches)
      {
         var word = match.Value;
         words.Add(word);
      }
      var wordArray = words.ToArray();
      predicate = predicate.And(x => x.LastName.ContainsAny(wordArray) || x.FirstName.ContainsAny(wordArray) || x.MiddleName.ContainsAny(wordArray));
   }
   return predicate;
}

There is similar question C# LINQ to Entities does not recognize the method 'Boolean'

But I would like to optimize this part somehow

 predicate = predicate.And(x => x.LastName.ContainsAny(wordArray) || x.FirstName.ContainsAny(wordArray) || x.MiddleName.ContainsAny(wordArray));

Any clue people?


回答1:


Oh! I just did it!

foreach (Match match in matches)
{
   var word = match.Value;                   
   predicate = predicate.And(x => x.LastName.Contains(word) || x.FirstName.Contains(word) || x.MiddleName.Contains(word));
}



回答2:


You can query against list of word in following way :

 predicate = predicate.And(x => wordArray.Contains(x.LastName) || wordArray.Contains(x.FirstName) || wordArray.Contains(x.MiddleName));


来源:https://stackoverflow.com/questions/47342632/linq-to-entities-does-not-recognize-the-method-boolean-containsany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!