The Sandbox Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Fuzzy Matching with Linq and Extension methods

Back to Forum: The Sandbox
  • User profile image

    Public Class FuzzyLookupForm 
    Dim dc As New DataClasses1DataContext 
    Private Sub UserInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UserInput.KeyDown 
    If e.KeyCode = Keys.Enter Then 
    'Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator. 
    'HACK: cast customers to AsEnumerable. 
    Dim customers = (From x In dc.oe_customers _ 
    Let Location = & " " & x.addr1.Trim & " " & & " " &, 5) _ 
    Select x.cust_code, Location).AsEnumerable 
    'I made the Location a concation of a bunch of fields in order not to have to reaggerate them in linq, at a later step. 
    Dim q = From x In customers _ 
    Let matches = UserInput.Text.ToLower.Split(New Char() {" "c, vbTab}).IntersectContains(x.Location.ToLower.Split(" ")).Count _ 
    + UserInput.Text.ToLower.Split(New Char() {" "c, vbTab}).Intersect(x.Location.ToLower.Split(" ")).Count _ 
    Where matches > 0 _ 
    Select x.cust_code, x.Location, matches _ 
    Order By matches Descending 
    Me.DataGridView1.DataSource = q.ToArray 
    End If 
    End Sub 
    End Class 
    Module MatchingExtensions 
    Function IntersectContains(ByVal first As IEnumerable(Of String), ByVal second As IEnumerable(Of String)) As IEnumerable(Of String) 
    'If first containts Det it will match Detroit in second. 
    Return From f In first, s In second Where s.Contains(f) Select s Distinct 
    End Function 
    End Module 

  • User profile image

    I put the

    + TextBox1.Text.ToLower.Split(New Char() {" "c, vbTab}).Intersect(x.location.ToLower.Split(" ")).Count 
    in to double count the exact matches.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.