我的任務是將篩選機制添加到結果集中。我理解使用Linq中的Where子句進行過濾的基本概念,但是這有一個更好的方法來做到這一點,對吧?使用Linq查詢進行VB.NET篩選
場景: 我在結果集上有5個可過濾的列。我必須在任何時候考慮這5個濾波器的所有變化。這意味着我必須手動輸入一個具有120種不同變體的If/ElseIf語句!
下面是事情進展的一般方法,我只是簡單地問:有沒有更好,更快的方法來做到這一點?
的過濾器
ByVal SearchMxMID As Integer,
ByVal SearchProfile As Integer,
ByVal SearchCZ As String,
ByVal SearchTerm As Integer,
ByVal SearchFwMth As Integer
的If語句的過程
If SearchMxMID = 0
And SearchProfile = 0
And SearchCZ = "All"
And SearchTerm = 0
And SearchFwMth = 0 Then
Dim queryMM = (From a In DBPricingContext.tbl_Matrix_Margins
Where a.CompanyID = CInt(sCompanyID)
Order By a.Profile
Select a)
Return New StoreResult(queryMM)
ElseIf SearchMxMID > 0
And SearchProfile = 0
And SearchCZ = "All"
And SearchTerm = 0
And SearchFwMth = 0 Then
Dim queryMM = (From a In DBPricingContext.tbl_Matrix_Margins
Where a.CompanyID = CInt(sCompanyID) And a.MarGroupID = SearchMxMID
Order By a.Profile
Select a)
Return New StoreResult(queryMM)
ETC ETC ETC
120總次數爲這5個過濾器的每個組合(無論它們是否是空白或有價值)。有沒有更快的方法,我可以做到這一點,可能在一個單一的Linq查詢?
If SearchMxMID > 0 Then a.MarGroupID = SearchMxMID Else DO NOT APPLY WHERE CLAUSE
???
**場景:我的結果集上有5個可篩選的列。我必須在任何時候考慮這5個濾波器的所有變化。這意味着我必須用120種不同的變體手動輸入一個If/ElseIf語句!**不,你不知道。只使用相關的東西 - 是的,你的where塊有五個IF語句,但你只需要插入值。我一直在SQL中這樣做,所以它應該也可以在Linq中使用。我在SQL中使用IN,在Linq中有類似的功能。 – Tim