2012-01-06 57 views
1

我需要考慮多個條件才能獲得價值。 我的意思是,如果所有條件都是真的,必須給我一個過濾的答案。 或者其中一個是真的,其餘都是假的... 所以我需要寫所有的可能性? 如果(一個& b &Ç& d)否則如果(一個& b & C)否則如果(一個&ç& d)否則如果(B &Ç& d)否則如果(一個& B)否則如果(一個& c)...等等:)))有沒有更簡單的方法來做到這一點?多個條件Linq擴展

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 
    } 

回答

3

只需添加一個凡在同一時間

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

它就像一個魅力!非常感謝您花時間幫助我。 – blackraist 2012-01-06 15:06:58

+0

這不是第一次從主表中引出所有100萬個項目,然後在應用程序中應用過濾器? 。 。 。或者如果我們要運行SQL事件探查器,我們會看到數據庫被打上了應用的過濾器,只有符合過濾條件的行纔會返回到應用程序(例如,只有100行中的50行符合我們的標準) ? – 2012-09-23 15:21:13

+1

@AshishAnand因爲Linq使用延遲執行,直到調用'.ToList()',查詢纔會被執行。 – cadrell0 2012-09-24 18:21:19

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()中的表達式。

+0

predicate.And條件不工作 – Prabu 2013-04-24 14:01:30