LINQ:构建查询以查询多个选项

时间:2013-01-31 03:48:40

标签: linq entity-framework dynamic

我正在动态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))

0 个答案:

没有答案