使用Linq查询进行VB.NET过滤

时间:2015-12-21 17:06:13

标签: vb.net linq

我的任务是为结果集添加过滤机制。我理解使用Linq中的Where子句进行过滤的基本概念,但是有更好的方法来做到这一点,对吗?

方案: 我的结果集中有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

这5个过滤器的每个组合总共120次(无论它们是空白还是有值)。有没有更快的方法可以做到这一点,可能在一个单一的Linq查询?

If SearchMxMID > 0 Then a.MarGroupID = SearchMxMID Else DO NOT APPLY WHERE CLAUSE

???

5 个答案:

答案 0 :(得分:0)

Linq查询是可链接的。根据你的情况,你可以简单地使用一个if,如果是真的话,那就添加另一个。即(在C#中但对于任何.Net编码器应该很容易遵循 - 或者您可以使用Telerik的代码转换器并稍微修改代码):

string country = "Germany";
string city = "Berlin";
int? productId = 3;

var data = db.Customers;

var result = data.AsQueryable();
if (!string.IsNullOrEmpty(country))
{
  result = result.Where(r => r.Country == country);
}
if (!string.IsNullOrEmpty(city))
{
  result = result.Where(r => r.City == city);
}
if (productId.HasValue)
{
  result = result
  .Where(r =>
     r.Orders.Any(o =>
       o.OrderDetails
        .Any(od => od.ProductID == productId.Value)));

}

另一种方法是简单地使用NULL或...方法。即:

Private Sub Main()
    Dim country As String = ""  '"Brazil"
    Dim city As String = "" '"Sao Paulo"
  Dim orderDate As System.Nullable(Of DateTime) = Nothing   'new DateTime(1996,8,28);

    Dim data = Orders.Where(Function(c) _
     String.IsNullOrEmpty(country) OrElse c.ShipCountry.StartsWith(country)) _
     .Where(Function(c) String.IsNullOrEmpty(city) OrElse c.ShipCity.StartsWith(city)) _
     .Where(Function(c) orderDate Is Nothing OrElse c.OrderDate = orderDate) _
     .Select(Function(c) New Order() With { _
         .OrderId = c.OrderID, _
         .CustomerId = c.CustomerID, _
         .OrderDate = c.OrderDate, _
         .ShipCountry = c.ShipCountry, _
         .ShipCity = c.ShipCity _
    })

    Dim f As New Form() With { .Text = "Query Results" }
    Dim dgv As New DataGridView() With { .Dock = DockStyle.Fill }
    f.Controls.Add(dgv)

    dgv.DataSource = data.ToList()
    f.ShowDialog()
End Sub

Public Class Order
    Public Property OrderId() As Integer
        Get
            Return m_OrderId
        End Get
        Set
            m_OrderId = Value
        End Set
    End Property
    Private m_OrderId As Integer
    Public Property OrderDate() As System.Nullable(Of DateTime)
        Get
            Return m_OrderDate
        End Get
        Set
            m_OrderDate = Value
        End Set
    End Property
    Private m_OrderDate As System.Nullable(Of DateTime)
    Public Property CustomerId() As String
        Get
            Return m_CustomerId
        End Get
        Set
            m_CustomerId = Value
        End Set
    End Property
    Private m_CustomerId As String
    Public Property ShipCountry() As String
        Get
            Return m_ShipCountry
        End Get
        Set
            m_ShipCountry = Value
        End Set
    End Property
    Private m_ShipCountry As String
    Public Property ShipCity() As String
        Get
            Return m_ShipCity
        End Get
        Set
            m_ShipCity = Value
        End Set
    End Property
    Private m_ShipCity As String
End Class

注意:代码是Linq To SQL代码。对于EF,这将不受支持,您需要使用前一种方法。

答案 1 :(得分:0)

您可以链接Where子句并动态构造查询,如下所示:

Dim query = DBPricingContext.tbl_Matrix_Margins.AsQueryable()

If compID > 0 Then
    query = query.Where(Function(a) a.CompanyID = compID)
End If
If SearchMxMID > 0 Then
    query = query.Where(Function(a) a.MarGroupID = SearchMxMID)
End If
...

query = query.OrderBy(Function(a) a.Profile)

请注意,我使用的是扩展方法语法和lambda表达式,而不是专门的LINQ查询语法。如果您只是选择参数Select本身,则无需拨打a

如果您使用的是LINQ-to-Objects而不是LINQ-to-some_database,请将AsQueryable()替换为AsEnumerable()

答案 2 :(得分:0)

您还可以使用LINQ语法串行编写查询。

Dim TempQuery = (From a In DbPricingContext.tbl_Matrix_Margins
                 Where a.CompanyID = CInt(sCompanyID)
                 Select a)

If SearchMxMID > 0 Then
    TempQuery = (From a In TempQuery
                 Where a.MarGroupID = SearchMxMID
                 Select a)
End If

'... Add your other conditions in here...

Return New StoreResult(From a In TempQuery Order By a.Profile Select a)

答案 3 :(得分:0)

使用SearchValue = DefaultValue OrElse SearchValue = PropertyValue

之类的逻辑
Dim result = DBPricingContext.tbl_Matrix_Margins.
                 Where(Function(a) a.CompanyID = CInt(sCompanyID)).
                 Where(Function(a) SearchMxMID = 0 OrElse a.MarGroupID = SearchMxMID)

以相同的方式添加其他搜索条件 如果SearchMxMID = 0,它将返回所有行,如果SearchMxMID <> 0

,则仅返回匹配的行

为了获得准确性结果,我将使用Nullable类型

Where(Function(a) SearchMxMID.HasValue = False OrElse a.MarGroupID = SearchMxMID.Value)

答案 4 :(得分:0)

您可以实施规范模式。像这样:

Sub Main()
        Dim someList As New List(Of SomeClass)

        Dim now As DateTime = DateTime.Now

        someList.Add(New SomeClass() With {.Id = 1, .Title = "001", .EntryDate = now})
        someList.Add(New SomeClass() With {.Id = 2, .Title = "002", .EntryDate = now.AddSeconds(10)})
        someList.Add(New SomeClass() With {.Id = 3, .Title = "003", .EntryDate = now.AddSeconds(20)})

        Dim idParam As Integer = 1
        Dim titleParam As String = "" '"001"
        Dim dateParam As DateTime = now



        ' first approach, one selector
        Dim selector As Func(Of SomeClass, Boolean) = Function(item)
                                                          With item
                                                              Return ((idParam <= 0) OrElse (.Id = idParam)) AndAlso
                                                                  ((String.IsNullOrEmpty(titleParam)) OrElse (.Title = titleParam)) AndAlso
                                                                  ((dateParam.CompareTo(DateTime.MinValue) = 0) OrElse (.EntryDate = dateParam))
                                                          End With
                                                      End Function



        Dim list = From o In someList Where selector(o) Select o

        For Each o In list
            Console.WriteLine(o)
        Next



        ' second approach, one selector per parameter
        Dim selectorId As Func(Of SomeClass, Boolean) = Function(item)
                                                            Return ((idParam <= 0) OrElse (item.Id = idParam))
                                                        End Function


        Dim selectorTitle As Func(Of SomeClass, Boolean) = Function(item)
                                                               Return ((String.IsNullOrEmpty(titleParam)) OrElse (item.Title = titleParam))
                                                           End Function


        Dim selectorEntryDate As Func(Of SomeClass, Boolean) = Function(item)
                                                                   Return ((dateParam.CompareTo(DateTime.MinValue) = 0) OrElse (item.EntryDate = dateParam))
                                                               End Function

        Dim list2 = From o In someList
                    Where
                    selectorId(o) AndAlso
                    selectorTitle(o) AndAlso
                    selectorEntryDate(o)
                    Select o

        For Each o In list2
            Console.WriteLine(o)
        Next

        Console.ReadLine()
    End Sub

    Public Class SomeClass
        Public Property Id As Integer
        Public Property Title As String
        Public Property EntryDate As DateTime

        Public Overrides Function ToString() As String
            Return String.Format("Id:{0}  Title:{1}  EntryDate:{2}", Id, Title, EntryDate)
        End Function
    End Class

希望这个例子对你有所帮助。