“If Any Contains Any” (DbExpressionBinding require

2019-07-19 08:20发布

I'm trying to build a simple search where I pass a list of keywords. But soon as I add the "any contains keywords" as a list instead of a string I get:

"DbExpressionBinding requires an input expression with a collection ResultType."

I have extended IQueryable<Inspector> with:

 public static IQueryable<Inspector> Search(this IQueryable<Inspector> qry, List<string> keywords)
 {
    return from i in qry
           where
             i.LastName.Any(x => keywords.Contains(i.LastName)) ||
             i.FirstName.Any(x => keywords.Contains(i.FirstName)) ||
             i.City.Any(x => keywords.Contains(i.City)) ||
           select i;
 }

And when I call it I use:

return qry.Search(keywords).ToList();

How should I fix this issue?

1条回答
贪生不怕死
2楼-- · 2019-07-19 09:03

I think you're trying to get at this:

return from i in qry
    where keywords.Contains(i.LastName) ||
        keywords.Contains(i.FirstName) ||  
        keywords.Contains(i.City)
    select i;

This will return you any records where the FirstName, LastName, or City is in your keyword list.

查看更多
登录 后发表回答