blackraist
blackraist

Reputation: 183

multiple conditions Linq extended

I need to consider multiple conditions to get value. i mean if all conditions true that must give me a filtered answer. Or one of them is true,rest are false... so i need to write all possibilities?? if(a&b&c&d) else if(a&b&c) else if(a&c&d) else if(b&c&d) else if(a&b) else if (a&c)...etc ?? :))) Is there a shorter way to do this?

    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
    }

Upvotes: 1

Views: 1605

Answers (2)

cadrell0
cadrell0

Reputation: 17307

Just add one Where condition at a time

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();
}

Upvotes: 3

Gert Arnold
Gert Arnold

Reputation: 109080

You can build an expression with Expression.And like:

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;
}

Note: I put r => r.ID == r.ID here as first expression, just to have an expression to start with. This snippet does not fully cover your code, but I think it is enough as an example.

Use the expression in return query.Where(expression).ToList().

Upvotes: 1

Related Questions