我正在动态LINQ文件(dynamic.cs)的帮助下使用以下方法构建动态查询:
Dim qs As New List(Of KeyValuePair(Of String, Object))
qs.Add(New KeyValuePair(Of String, Object)("Price >= @", 10))
qs.Add(New KeyValuePair(Of String, Object)("Price <= @", 50))
qs.Add(New KeyValuePair(Of String, Object)("GoLive= @", False))
qs.Add(New KeyValuePair(Of String, Object)("Format = @", "DVD"))
qs.Add(New KeyValuePair(Of String, Object)("Format = @", "BLR"))
Dim products = From p In data.Products
Select p
For Each q In qs
products = products.Where(q.Key, q.Value)
Next
Dim results = products.ToList
这是你期望的,但是看看我如何在'格式'字段中寻找'DVD'和'BLR'? 这会查询BOTH选项,而不是。
有没有办法使用上面的查询构建方法以某种方式查询任一选项,或者最好组合“格式”字段,如下所示:
qs.Add(New KeyValuePair(Of String, Object)("Format = @ OR Format = ", both options here))