0

我有以下方法可以为我返回一些数据:

public List<Document> GetDocumentsList(Guid sessionID, bool includeValid, bool includeExpired, bool includeAboutToExpire)
{
   using (DB db = new DB())
   {
       // get the active documents
       var docs = db.Documents
                  .Where(d =>
                      db.EmployeeStatuses
                     .Any(s => s.EmpID == d.EmpID && s.StatusEndDate == null && s.Status == "Active")
                        );

        // how to filter the result depending on includeValid, includeExpired and includeAboutToExpired parameters?

        return docs.ToList()
    }
}

这里的问题是,我想根据bool参数过滤结果,例如,如果includeValid为 true,则将包含有效文档,如果为 true,则将包含includeExpired过期文档等等,我可以同时拥有这三个真实的或其中一两个是真实的。我不想多次调用数据库。有没有一种逻辑可以通过一个电话来做到这一点?

4

4 回答 4

5

LinQ to entity 推迟执行,直到结果被枚举。因此,添加后续.Where()语句不会导致多次调用 db

var docs = db.Documents
                  .Where(d =>
                      db.EmployeeStatuses
                     .Any(s => s.EmpID == d.EmpID && s.StatusEndDate == null && s.Status == "Active")
                        );

if (includeValid)
   docs = docs.Where(x => [condition]);

//... and so on.

return docs.ToList(); // <- This is where the query is actually executed. 
于 2013-02-04T16:07:26.377 回答
1

您可以使用该PredicateBuilder实现。使用此类,您可以在 linq 查询上应用一些过滤器。像这样的东西:

public IEnumerable<Document> GetDocumentsList(Guid? sessionID, bool? includeValid, bool? includeExpired, bool?  includeAboutToExpire)
{
    var query = db.Documents;

    if (sessionID.HasValue)
       query = query.And(x => x.SessionID = sessionID.Value);

    if (includeValid.HasValue && includeValid.Value)
       query = query.And(x => x.IncludeValid = includeValid.Value);

    // others parameters...

    return query.ToList();
}

PredicateBuilder 实现

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

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);
  }
}
于 2013-02-04T16:14:37.947 回答
0

看起来您需要加入文档和状态。条件(!includeValid || d.Valid)还可以让您过滤有效文档,只有 idincludeValidtrue

public List<Document> GetDocumentsList(Guid sessionID, 
       bool includeValid, bool includeExpired, bool includeAboutToExpire)
{
   using (DB db = new DB())
   {
       // get the active documents
       var docs = from d in db.Documents
                  join s in db.EmployeeStatuses on d.EmpID equals s.EmpID
                  where s.StatusEndDate == null &&
                        s.Status == "Active" &&
                        (!includeValid || d.Valid) &&
                        (!includeExpired || d.Expired) &&
                        (!includeAboutToExpired || d.AboutToExpired)
                  select d;

        return docs.ToList()
    }
}
于 2013-02-04T16:16:39.963 回答
0

what about something like this? (I'm making assumptions on how those flags are supposed to be evaluated of course).

public List<Document> GetDocumentsList(Guid sessionID, bool includeValid, bool includeExpired, bool includeAboutToExpire)
{
   using (DB db = new DB())
   {
       // get the active documents
       var docs = db.Documents
                  .Where(d =>
                      db.EmployeeStatuses
                     .Any(s => s.EmpID == d.EmpID 
                               && s.StatusEndDate == null 
                               && (s.Status == "Active"
                                 || (includeValid && s.Status == "Valid")
                                 || (includeExpired && s.Status == "Expired")
                                 || (includeAboutToExpired && s.Status == "AboutToExpired") ))
                        );

        // how to filter the result depending on includeValid, includeExpired and includeAboutToExpired parameters?

        return docs.ToList()
    }
}
于 2013-02-05T11:55:45.743 回答