LINQ to Entities无法识别Boolean ContainsAny方法

时间:2017-11-17 03:04:55

标签: c# linq entity-framework-6

错误

  

LINQ to Entities无法识别方法'布尔值   ContainsAny(System.String,System.String [])'方法,以及此方法   无法翻译成商店表达。

代码

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;
}

有类似问题C# LINQ to Entities does not recognize the method 'Boolean'

但我想以某种方式优化这部分

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

任何线索的人?

2 个答案:

答案 0 :(得分:0)

哦!我刚刚做到了!

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));
}

答案 1 :(得分:0)

您可以通过以下方式查询单词列表:

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