动态添加LINQ查询中的WHERE子句 [英] Adding WHERE clause in LINQ query dynamically

查看:74
本文介绍了动态添加LINQ查询中的WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个通用列表,其中包含大约250个结果。这些结果是某些类的集合。该类包含标题,关键字等属性。现在在这个通用列表中,我想找到完全匹配,所有单词匹配和一个或多个单词匹配的结果。例如,在结果集中,某些结果的Title包含值为test linq sample,linq sample,sample test linq,sample test等等。现在我想使用LINQ在此列表中应用过滤器用VB.NET。比方说,我的搜索词是样本测试linq,作为回报我需要以下结果



1)EXACT匹配的结果。也就是说,那些结果标题包含样本测试linq

2)所有单词匹配的结果。也就是说,那些结果,其中Title包含来自sample test linq的所有单词(通过在LINQ查询中使用AND条件)

3)一个或多个单词Match的结果。也就是说,标题包含标题中任何单词的结果(通过在LINQ查询中使用OR条件)



有人可以知道如何实现这一点通过使用LINQ与VB.NET代码。这里我的搜索词被更改,即它包含一个或多个单词。为此,我需要在运行时通过在某些函数中编写逻辑来动态创建LINQ的WHERE子句。



Dim query = from result in listResultSet

哪里有条件



我对此有很多关注,但我得到的是两条链接以及与C#相关的代码。我需要的是VB.NET代码



http://www.albahari.com/nutshell/predicatebuilder.aspx



http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx



谢谢

Hi All,

I have a generic list which contains around 250 results. These results are the collection of some class. That class contains properties like Title, Keyword and etc. Now on this generic list I want to find out the results with "Exact match", "All words match" and "One or more word match". For example, in result set some results have Title containing values as "test linq sample", "linq sample", "sample test linq" , "sample test" and etc. Now I want to apply a filter on this list by using LINQ with VB.NET. Let say, my search term is "sample test linq" and in return I need results on following

1) Results for EXACT match. That is, those result where Title contains "sample test linq"
2) Result for ALL word Match. That is, those results where Title contains all the words from "sample test linq" (By using AND condition in LINQ query)
3) Result for One or more word Match. That is, those results where Title contains any of the word present in title (By using OR condition in LINQ query)

Can some one has any idea how to achieve this by using LINQ with VB.NET code. Here my search term gets changed i.e it contains one or more words. For this I need to dynamically create WHERE clause of LINQ at runtime, by writing the logic in some function.

Dim query = From result in listResultSet
Where "Conditions"

I have goggled a lot on this but what I am getting is below two links and the code that is related to C#. What I need is VB.NET code

http://www.albahari.com/nutshell/predicatebuilder.aspx

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Thanks

推荐答案

你说
I have a generic list which contains around 250 results.

这意味着你正在使用 LINQ to Objects ,所以你似乎不需要构造一个将被发送到SQL服务器来执行的查询为了效率。查询将在您的代码中执行。 (250个项目的集合大集合。)

我可能只为3个案例选择3个不同的查询。

它比动态构造查询更有效,几乎可以肯定更容易实现和维护。

This implies that you are using LINQ to Objects, so you don't seem to need to construct a query that will be "sent off" to a SQL server to execute for efficiency. The query will execute in your code. (And a collection of 250 items is not a large collection.)
I'd probably just choose between 3 different queries for the 3 cases.
It is going to be more efficient than dynamically constructing the queries, and almost certainly a whole lot easier to implement and maintain.


Where子句需要一个布尔值。您可以通过功能提供。以下是基于我对您的问题的理解的示例。您可以为生成动态Where子句所需的任何参数提供该函数。



The "Where" clause requires a boolean value. You can supply that with a function. Here is an example based on my understanding of your issue. You can feed that function whatever arguments you need to generate your "dynamic Where" clause.

Private Sub Test()
   Dim items As New List(Of Item)
   items.Add(New Item("this is a test"))
   items.Add(New Item("this is not a test"))
   items.Add(New Item("this is maybe a test"))
   items.Add(New Item("something else"))
   items.Add(New Item("something else test"))

   Dim lookfor As String = "this is a test"

   Console.WriteLine("Exact Match: " & lookfor)
   For Each itm As Item In GetMatchedItems(items, lookfor, MatchType.Exact)
      Console.WriteLine(itm.Title)
   Next itm

   Console.WriteLine("----------------------")
   Console.WriteLine("Contains all: " & lookfor)
   For Each itm As Item In GetMatchedItems(items, lookfor, MatchType.ContainsAll)
      Console.WriteLine(itm.Title)
   Next itm

   Console.WriteLine("----------------------")
   lookfor = "else not"
   Console.WriteLine("Contains one or more: " & lookfor)
   For Each itm As Item In GetMatchedItems(items, lookfor, MatchType.ContainsOneOrMore)
      Console.WriteLine(itm.Title)
   Next itm
End Sub

Private Function GetMatchedItems(ByVal source As List(Of Item), ByVal lookfor As String, ByVal mt As MatchType) As IEnumerable(Of Item)
   Return From el In source _
          Let itm As Item = DirectCast(el, Item) _
          Where MatchTester(itm.Title, lookfor, mt) _
          Select itm
End Function

Private Function MatchTester(ByVal teststring As String, ByVal lookfor As String, ByVal match As MatchType) As Boolean
   If Not [Enum].IsDefined(GetType(MatchType), match) Then
      Throw New ArgumentException("Invalid MatchType")
   End If

   Select Case match
      Case MatchType.Exact
         MatchTester = String.Equals(teststring, lookfor)

      Case MatchType.ContainsAll, MatchType.ContainsOneOrMore
         Dim source As New List(Of String)(teststring.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))
         Dim findthese As New List(Of String)(lookfor.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))

         If match = MatchType.ContainsAll Then
            MatchTester = True
            For Each word As String In findthese
               If Not source.Contains(word) Then
                  MatchTester = False
                  Exit For
               End If
            Next word
         Else
            MatchTester = False
            For Each word As String In findthese
               If source.Contains(word) Then
                  MatchTester = True
                  Exit For
               End If
            Next
         End If
   End Select
End Function

Private Enum MatchType
   Exact
   ContainsAll
   ContainsOneOrMore
End Enum 'MatchType

Private Class Item
   Public Sub New(ByVal Title As String)
      Me._Title = Title
   End Sub

   Private _Title As String
   Public Property Title() As String
      Get
         Return _Title
      End Get
      Set(ByVal value As String)
         _Title = value
      End Set
   End Property 'Title
End Class ' Item


这篇关于动态添加LINQ查询中的WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆