4

我正在尝试将一些直接构建 SQL 查询的旧代码转换为实体框架,并且遇到了许多似乎都有的问题(从围绕该主题的大量问题来看):如何在 linq 中表达动态 where 条件。

我如何用 linq 查询表达以下代码:

    switch (status) {
        case "0":
            sqlwhere = " WHERE status < 0 ";
            break;
        case "-1":
            sqlwhere = " WHERE status = -1 ";
            break;
        case "-100":
            sqlwhere = " WHERE status = -100 ";
            break;
        case "1":
        default:
            sqlwhere = " WHERE status >= 0 ";
            break;
    }

    if (strsearch != "")
        sqlwhere += " AND desc LIKE '%" + strsearch + "%' ";

    string sqlc = "SELECT top 10 * FROM c " + sqlwhere + " order by date desc";

我已经阅读了PredicateBuilder其他帖子中的动态 Linq 扩展,但我认为像这样的简单案例可以在没有外部库的情况下解决。

使用 .net 4.5、EF 5.0、C#,是否可以以“动态”方式完成,而无需为每个案例构建完整的 linq 语句?

4

3 回答 3

2

在您的情况下,使用PredicateBuilder如下所示

另请查看我的博客文章:使用 Linq 进行动态查询

var outer = PredicateBuilder.True<Entity>();

switch (status) {
        case "0":
            outer = outer.And (p => p.status<0);
            break;
        case "-1":
            outer = outer.And (p => p.status==-1);
            break;
        case "-100":
            outer = outer.And (p => p.status==-100);
            break;
        case "1":
        default:
            outer = outer.And (p => p.status>=0); 
            break;
    }

if (strsearch != "")
        outer = outer.And (p => p.desc.Contains(strsearch ));

dataContext.Entity.Where (outer );
于 2012-11-05T13:52:02.993 回答
2

如果您不想使用外部的东西,那么只需使用 fluent API:

var query = db.YourTableName
              .Where(x => x.desc.Contains(strsearch));

switch (status) {
   case "0":
        query = query.Where(x => x.status < 0);
        break;
   case "-1":
        query = query.Where(x => x.status == -1);
        break;
   case "-100":
        query = query.Where(x => x.status == -100);
        break;
   case "1":
   default:
        query = query.Where(x => x.status >= 0);
        break;
}

var result = query.OrderByDescending(x => x.date)
                  .Take(10);

顺便说一句,您可以创建按状态过滤的扩展方法。您的查询将如下所示:

var query = db.YourTableName
              .FilterByStatus(status)
              .Where(x => x.desc.Contains(strsearch))
              .OrderByDescending(x => x.date)
              .Take(10);

扩展方法:

public static IQueryable<YourType> FilterByStatus(this IQueryable<YourType> query, 
                                                  string status)
{

    switch (status) {
       case "0":
            return query.Where(x => x.status < 0);            
       case "-1":
            return query.Where(x => x.status == -1);
       case "-100":
            return query.Where(x => x.status == -100);
       case "1":
       default:
            return query.Where(x => x.status >= 0);
    }
}
于 2012-11-05T14:12:18.677 回答
0

您可以将LinkExtensionLinqKit一起使用

  using (var context = new workEntities() )
{

    Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
    dictionary["Title"] = new List<string> {  
                    "Network Engineer", 
                    "Security Specialist", 
                    "=Web Developer"
                };
    dictionary["Salary"] = new List<string> { ">=2000" };
    dictionary["VacationHours"] = new List<string> { ">21" };
    dictionary["SickLeaveHours"] = new List<string> { "<5" };                
    dictionary["HireDate"] = new List<string> { 
                    ">=01/01/2000",
                    "28/02/2014" 
                };
    dictionary["ModifiedDate"] = new List<string> { DateTime.Now.ToString() };

    var data = context.Employee.CollectionToQuery(dictionary).ToList();
}
于 2014-03-02T09:38:44.663 回答