LINQ SqlMethods.Like(和.Contains)失败

时间:2016-11-16 17:11:32

标签: vb.net linq filter contains

我有一个名为Person的类,它具有属性FirstNameLastNameMiddleName,我有一个名为{的整个表单的SortedDictionary(Of Integer,Person) {1}}。

oPeople上,我调用一个加载65人列表的方法。现在这是硬编码的,但最终我会从数据库中抓取它。

加载表单后,我有一个名为Form_Load的{​​{1}},用户可以输入搜索字词并让系统通过TextBox过滤txtSearchForName完全匹配或部分匹配(不区分大小写)。

最终我希望能够搜索FirstName,LastName和MiddleName(如果有的话)之间的比较。

此时我要做的就是遍历LINQ查询的结果并将它们输出到控制台窗口。

这是oPeople类:

LastName

这是我用来添加人的方法。我正在增加65人,但已经减少了代码:

Person

这是我的LINQ语句,后跟输出到控制台,当用户单击按钮时调用该控件:

Public Class Person

    Private _fnm As String = String.Empty
    Public Property FirstName() As String
        Get
            Return _fnm
        End Get
        Set(ByVal value As String)
            _fnm = value.Trim
        End Set
    End Property

    Private _lnm As String = String.Empty
    Public Property LastName() As String
        Get
            Return _lnm
        End Get
        Set(ByVal value As String)
            _lnm = value.Trim
        End Set
    End Property

    Private _mnm As String = String.Empty
    Public Property MiddleName() As String
        Get
            Return _mnm
        End Get
        Set(ByVal value As String)
            _mnm = value.Trim
        End Set
     End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal firstName As String,
                   ByVal lastName As String,
                   Optional ByVal middleName As String = "")

        _fnm = firstName
        _lnm = lastName
        _mnm = middleName

    End Sub

End Class

LINQ语句按原样工作,没有条件,因此它循环并正确列出Private Sub FillPeopleDictionary() Try If oPeople.Count > 0 Then oPeople.Clear() Dim oNewPerson As Person = Nothing oNewPerson = New Person("Scarlett", "Johansson") oPeople.Add(1, oNewPerson) oNewPerson = New Person("Amy", "Adams") oPeople.Add(2, oNewPerson) oNewPerson = New Person("Jessica", "Biel") oPeople.Add(3, oNewPerson) Catch ex As Exception MessageBox.Show(ex.Message, "Error [FillPeopleDictionary]", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub 集合中的所有人。

在初始Dim sSearchTerm As String = txtSearchForName.Text.Trim.ToLower Dim queryResults = From person In oPeople 'Where SqlMethods.Like(person.Value.LastName.ToLower, "%" & sSearchTerm & "%") 'Where person.Value.LastName.ToLower.Contains("%" & sSearchTerm & "%") Console.WriteLine("search term: " & sSearchTerm & Environment.NewLine & Environment.NewLine & "queryResults.Count: " & queryResults.Count.ToString & Environment.NewLine) For Each result In queryResults If Not String.IsNullOrEmpty(result.Value.MiddleName) Then Console.WriteLine(result.Key.ToString.PadLeft(2, "0") & ": " & result.Value.FirstName & " " & result.Value.MiddleName & " " & result.Value.LastName) Else Console.WriteLine(result.Key.ToString.PadLeft(2, "0") & ": " & result.Value.FirstName & " " & result.Value.LastName) End If Next 语句下方注释了两个oPeople子句。这是我试图过滤的两种方式。一种方法是使用Where,另一种方法是使用queryResults但不起作用。

如果用户输入“mar”,我希望从65列表(不区分大小写)中找回6个人的列表:

  Meghan Markle   Margo Robbie
  凯特玛拉   玛丽伊丽莎白温斯特德   玛丽安里维拉
  艾米智能

现在当然是在.Contains.Like上搜索。现在我只想让FirstName工作。只有LastName列表才会出现:

  Meghan Markle   凯特玛拉   艾米智能

谁能看到我在这里做错了什么?或者我应该放弃使用带有SortedDictionary的LINQ的想法?

3 个答案:

答案 0 :(得分:0)

您的第二个Where子句尝试已结束,但Contains函数除String.Contains之外没有使用SQL使用的%通配符,因此您需要:

Dim queryResults =
    From person In oPeople
    Where person.Value.LastName.ToLower.Contains(sSearchTerm)

您可以使用FirstName轻松添加OrElse person.Value.FirstName.ToLower.Contains(sSearchTerm)支票。

答案 1 :(得分:0)

将您的Person课程更改为包含PersonId,然后将其传递给oNewPerson = New Person(1, "Scarlett", "Johansson")

将oPeople更改为List(Of Person),因此在添加时看起来像oPeople.Add(oNewPerson)

您的LINQ语句将如下所示:

Dim queryResults = From person In oPeople
                   Where person.FirstName.ToLower Like "*" & sSearchTerm & "*" Or
                         person.LastName.ToLower Like "*" & sSearchTerm & "*"

您还希望将其余部分更改为不再使用字典:

For Each result In queryResults

    If Not String.IsNullOrEmpty(result.MiddleName) Then

        Console.WriteLine(result.PersonId.ToString.PadLeft(2, CChar("0")) & ": " & result.FirstName & " " & result.MiddleName & " " & result.LastName)

    Else

        Console.WriteLine(result.PersonId.ToString.PadLeft(2, CChar("0")) & ": " & result.FirstName & " " & result.LastName)

    End If

Next

希望这有帮助。

答案 2 :(得分:0)

将Linq查询更改为:

Dim queryResults = From p In oPeople
                               Where p.Value.FirstName.ToLower.Contains(sSearchTerm) Or p.Value.LastName.ToLower.Contains(sSearchTerm)