Linq扩展了多个条件

时间:2012-01-06 13:22:42

标签: linq conditional-statements

我需要考虑多种条件才能获得价值。 我的意思是如果所有条件都是真的必须给我一个过滤的答案。 或者其中一个是真的,休息是假的...... 所以我需要写出所有可能性? if(a& b& c& d)else if(a& b& c)else if(a& c& d)else if(b& c& d)else else if(a& b)else if(a& b)else if(a& c) )......等等? :)))有没有更短的方法来做到这一点?

    public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName)
    {
        var db = new requestDBEntities();
        bool dp = !string.IsNullOrEmpty(departmant);
        bool pr = !string.IsNullOrEmpty(productName);
        bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate);
        bool rs = reqStateID > 0 ? true : false;

        var query = (from r in db.requests
                     select new ProductReqNoDate
                     {
                         departmant = r.departmant,
                         reqNo = r.reqNo,
                         reqDate = r.reqDate,
                         productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault()
                     }).ToList();
        if (dp & pr & tm & rs)
        {
            var rState = (from ta in db.reqStates
                          where ta.reqStateID == reqStateID && ta.isActive == true
                          select ta.reqNo).ToList();
            var prName = (from p in db.products where p.productName.Contains(productName) select p.reqNo).ToList();
            DateTime dtfirstDate = Convert.ToDateTime(firstDate);
            DateTime dtlastDate = Convert.ToDateTime(lastDate);

            return query.Where(
                r => rState.Contains(r.reqNo)  //find by Request State
                  && r.departmant == departmant  //find by Departmant
                   && (r.reqDate >= dtfirstDate && r.reqDate <= dtlastDate) //find by Date
                    && prName.Contains(r.reqNo) //Find By Product Name
                    ).ToList();

        }
        else if (dp & pr & tm) { /*return query.Where(...} */}
        else if (pr & tm & rs) { /*return query.Where(...} */}
        else if (dp & pr && rs) { /*return query.Where(...} */}
        else if (dp & pr) { /*return query.Where(...} */}
        //else if ...etc
    }

2 个答案:

答案 0 :(得分:3)

一次只添加一个Where条件

public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate, string productName)
{
    var db = new requestDBEntities();
    bool dp = !string.IsNullOrEmpty(departmant);
    bool pr = !string.IsNullOrEmpty(productName);
    bool tm = !string.IsNullOrEmpty(firstDate) && !string.IsNullOrEmpty(lastDate);
    bool rs = reqStateID > 0 ? true : false;

    var query = (from r in db.requests
                 select new ProductReqNoDate
                 {
                     departmant = r.departmant,
                     reqNo = r.reqNo,
                     reqDate = r.reqDate,
                     productName = (from p in db.products where p.reqNo == r.reqNo select p.productName).FirstOrDefault()
                 }).AsQueryable();  //AsQueryable is not always needed, but it shouldn't hurt and I don't feel like checking for this example.


    if (dp)
    {
        query = query.Where(q => /*condition*/);
    }

    if (pr)
    {
        query = query.Where(q => /*condition*/);
    }

    if (tm)
    {
        query = query.Where(q => /*condition*/);
    }

    if (rs)
    {
        query = query.Where(q => /*condition*/);
    }

    return query.ToList();
}

答案 1 :(得分:1)

您可以使用Expression.And构建一个表达式,如:

private Expression<Func<Request, bool>> GetPredicate(FilterDto filter)
{
  Expression<Func<Request, bool>> predicate = r => r.ID == r.ID;

  if (filter.Department.HasValue)
    predicate = predicate.And(r => r.Department == filter.Department.Value);
  if (filter.FirstDate.HasValue)
    predicate = predicate.And(r => (r.reqDate >= filter.FirstDate.Value));
  if (filter.LastDate.HasValue)
    predicate = predicate.And(r => (r.reqDate <= filter.LastDate.Value));

  /* ... */

  return predicate;
}

注意:我将r => r.ID == r.ID放在第一个表达式中,只是为了得到一个表达式。此代码段并未完全覆盖您的代码,但我认为这足以作为示例。

使用return query.Where(expression).ToList()中的表达式。