2013-05-18 105 views
0

我對此感到困惑,而且我知道會有更加靈活的方法來啓動它。 'result'變量有許多記錄,我想檢查IN_SiteId是否大於0並過濾它,對於LandownerId和PaymentCategoryId等等。如果我能得到前2的正確方法,我會從那裏繼續。這應該更容易,但有一個磚牆一天。任何意見讚賞使用參數篩選IQueryable結果集

public IQueryable rptRentPaidMonthly(int IN_SiteId, int IN_LandownerId, int IN_PaymentCategoryId, int IN_PaymentTypeId, string IN_ShowRelevantProportion) 
{ 

    var result = this._lmsDb.rptRentPaidMonthly(IN_daysFrom, IN_daysTo, IN_SiteId, IN_LandownerId, IN_PaymentCategoryId, IN_PaymentTypeId, IN_ShowRelevantProportion); 

    if (IN_SiteId > 0) 
    { 
     var searchResults = (from s in result 
          where (s.SiteId == @IN_SiteId) 
          select s); 

     return searchResults.AsQueryable(); 
    } 

    return result.AsQueryable(); 
} 

回答

0

我不是一個LINQ專家,但我認爲你可以做這樣的事情:

public IQueryable rptRentPaidMonthly(int IN_SiteId, int IN_LandownerId, int IN_PaymentCategoryId, int IN_PaymentTypeId, string IN_ShowRelevantProportion) 
{ 
    var result = this._lmsDb.rptRentPaidMonthly(IN_daysFrom, IN_daysTo, IN_SiteId, IN_LandownerId, IN_PaymentCategoryId, IN_PaymentTypeId, IN_ShowRelevantProportion); 


    var searchResults = (from s in result 
         where (IN_SiteId <= 0 || s.SiteId == IN_SiteId) 
           && (IN_LandownerId <= 0 || s.LandownerId == IN_LandownerId) 
           && (IN_PaymentCategoryId <= 0 || s.PaymentCategoryId == IN_PaymentCategoryId) 
           && (IN_PaymentTypeId <= 0 || s.PaymentTypeId == In_PaymentTypeId) 
         select s); 

    return searchResults.AsQueryable(); 
} 

where子句檢查每個過濾器的值小於或等於0,如果是,那麼它將返回true,並且不會評估嘗試過濾提供的值上的實際字段的下一位。

+0

當然 - 謝謝! – John