locked
VB linq list like case insensitive RRS feed

  • Question

  • I need this to be case insensitive. I've seen C# examples using StringComparison.OrdinalIgnoreCase but not sure how to use this here...

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where mess.Subject Like SearchFilter Select mess).OrderByDescending(Function(e) e.Date).ToList()

    Monday, July 3, 2017 6:03 AM

Answers

  • Thanks for responding I had already tried your suggestion. However, I went back and looked at this I found that depending on the field being compared ie mess.Subject ".ToLower" sometimes worked and sometimes the sub just exited without generating an error.

    I found this always works LCase(mess.Subject)

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where mess.Subject Like SearchFilter

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where LCase(mess.Subject)

    Like SearchFilter.ToLower Select mess).OrderByDescending(Function(e) e.Date).ToList()



    • Edited by NX10000 Monday, July 3, 2017 6:50 PM
    • Marked as answer by NX10000 Monday, July 3, 2017 7:13 PM
    Monday, July 3, 2017 6:44 PM

All replies

  • I need this to be case insensitive. I've seen C# examples using StringComparison.OrdinalIgnoreCase but not sure how to use this here...

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where mess.Subject Like SearchFilter Select mess).OrderByDescending(Function(e) e.Date).ToList()

    use ToLower() on both strings in the query:

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where mess.Subject.ToLower
    Like SearchFilter.ToLower Select mess).OrderByDescending(Function(e) e.Date).ToList()



    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 3, 2017 3:26 PM
  • Thanks for responding I had already tried your suggestion. However, I went back and looked at this I found that depending on the field being compared ie mess.Subject ".ToLower" sometimes worked and sometimes the sub just exited without generating an error.

    I found this always works LCase(mess.Subject)

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where mess.Subject Like SearchFilter

    Dim filteredMess1 As List(Of Message) = (From mess In m_Messages.objects Where LCase(mess.Subject)

    Like SearchFilter.ToLower Select mess).OrderByDescending(Function(e) e.Date).ToList()



    • Edited by NX10000 Monday, July 3, 2017 6:50 PM
    • Marked as answer by NX10000 Monday, July 3, 2017 7:13 PM
    Monday, July 3, 2017 6:44 PM
  • ToUpper will not work if the string is null.  It will give an exception which may be eaten by the linq command.

    Lloyd Sheen

    Monday, July 3, 2017 9:47 PM
  • Hello,

    For the record, here is how to work with OrdinalIgnoreCase in a mock up example. First result is 0, second is 2.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim messageList As New List(Of Message) From
                {
                    New Message With {.Subject = "1 message here"},
                    New Message With {.Subject = "1 subject"},
                    New Message With {.Subject = "MESSAGE line"}
                }
    
            Dim results = messageList.Where(Function(item) item.Subject.Contains1("aaa"))
            If results.Count > 1 Then
                MessageBox.Show($"Count is {results.Count}")
            Else
                MessageBox.Show("no match")
            End If
            results = messageList.Where(Function(item) item.Subject.Contains1("ssag"))
            If results.Count > 1 Then
                MessageBox.Show($"Count is {results.Count}")
            Else
                MessageBox.Show("no match")
            End If
        End Sub
    End Class
    Public Class Message
        Public Property Subject As String
        Public Property Body As String
    
    End Class
    Public Module StringExtensions
        <Runtime.CompilerServices.Extension>
        Public Function Contains1(
            ByVal sender As String,
            ByVal Value As String,
            Optional ByVal Comp As StringComparison = StringComparison.OrdinalIgnoreCase) As Boolean
    
            Return sender.IndexOf(Value, Comp) >= 0
    
        End Function
    End Module


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 3, 2017 10:37 PM