使用Entity Framework实现SQL“AND”搜索

时间:2011-04-25 19:53:46

标签: c# sql frameworks entity

我有以下SQL查询 -

SELECT * FROM dbo.LocalContacts WHERE name LIKE '%taiwan%' AND name LIKE '%mvp%'

如何使用EF实现它?我需要分解搜索字符串,然后对每个关键字进行“AND”搜索。我有以下但这不是我想要的 -

var searchTextLowerCase = Request.QueryString["q"].ToLower().Split(' ');
            foreach (var s in searchTextLowerCase)
            {
                foreach (var x in communities)
                {
                    if (
                        (!string.IsNullOrWhiteSpace(x.Name) && x.Name.ToLower().Contains(s)) ||
                        (!string.IsNullOrWhiteSpace(x.Acronym) && x.Acronym.ToLower().Contains(s)) ||
                        (!string.IsNullOrWhiteSpace(x.OwnerFirstName) && x.OwnerFirstName.ToLower().Contains(s)) ||
                        (!string.IsNullOrWhiteSpace(x.OwnerEmail) && x.OwnerEmail.ToLower().Contains(s)) ||
                        (!string.IsNullOrWhiteSpace(x.OwnerLastName) && x.OwnerLastName.ToLower().Contains(s)) ||
                        )
                    {
                        if (!filteredCommunities.Exists(y => y.Id == x.Id))
                            filteredCommunities.Add(x);
                    }
                }
            }

我可以动态构建SQL查询并对数据库执行它,但是我可以使用EF吗?

3 个答案:

答案 0 :(得分:2)

我没有针对数据库对此进行测试,但您应该能够创建实体框架模型,然后进行查询。您可以在条件中添加更多内容,以满足您的所有要求:

static void Main(string[] args)
{
    string holder = "A B C D E";
    string[] searchTextLowerCase = holder.ToLower().Split(' ');

    using (Model1Container context = new Model1Container())
    {
        var q = context.Communities;
        List<Community> communities = q.Where(c => searchTextLowerCase.Contains(c.Name)).ToList();
    }
}

答案 1 :(得分:2)

这是使用PredicateBuilder执行此操作的一种方法:

var predicate = PredicateBuilder.False<Community>();
foreach(var s in searchTextLowerCase)
{
    predicate = predicate.Or(x => x.Name.ToLower().Contains(s));
    predicate = predicate.Or(x => x.Acronym.ToLower().Contains(s))
    //.. etc
}
var filteredCommunities = communities.Where(predicate);

PredicateBuilder的来源:

public static class PredicateBuilder
    {
      public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
      public static Expression<Func<T, bool>> False<T> () { return f => false; }

      public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                          Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
      }

      public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                           Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
      }
    }

答案 2 :(得分:1)

以下是我如何使用它 -

IQueryable<Product> SearchProducts (params string[] keywords)
{
  IQueryable<Product> query = dataContext.Products;

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    query = query.Where (p => p.Description.Contains (temp));
  }
  return query;
}

Source