where 句に複数の値を含む LINQ クエリがあります。where句の値は、チェックボックスリストのフィルタリングオプションからのものです。Checkboxlist は Null (または例では空の文字列) を返すことができます。これは、checkboxlist で何も選択しないとすべてを選択することを意味するため、クエリに where 句が必要ないことを意味します。それを処理できる1つの優れたLINQを作成する方法がわからなかったため、以下のように複数のクエリで複数のIFステートメントを使用することになりました。私はそれをテストし、今のところwhere句に2つのパラメーターを使用して正常に動作します。しかし実際には、クエリに渡すにはさらに多くのパラメーターが必要であり、そのジョブを実行するために多くの IF ステートメントを使用すると面倒になります。1 つの適切な LINQ クエリでそれを処理するにはどうすればよいでしょうか?
Function FilterCol(ByVal col As List(Of ProductDetails), Optional SelectedCategory As List(Of String) = Nothing, Optional SelectedBrand As List(Of String) = Nothing) As List(Of ProductDetails)
Dim strSelectedCategory As String = String.Join(",", SelectedCategory .ToArray())
Dim strSelectedBrand As String = String.Join(",", SelectedBrand .ToArray())
If strSelectedCategory = "" And strSelectedBrand = "" Then
Return col
ElseIf strSelectedCategory = "" Then
Dim res1 As IEnumerable(Of StatsDetails) = From x In col Where strSelectedBrand.Contains(x.Brand) Select x
Return res1.ToList
ElseIf strSelectedBrand = "" Then
Dim res2 As IEnumerable(Of StatsDetails) = From x In col Where strSelectedCategory.Contains(x.Category) Select x
Return res2.ToList
Else
Dim res As IEnumerable(Of StatsDetails) = From x In col Where strSelectedCategory.Contains(x.Category) And strSelectedBrand.Contains(x.Brand) Select x
Return res.ToList
End If
End Function