Using "Like" in LINQ to SQL

I've recently discovered that LINQ and VB can be confusing. I was attempting to pull the top 10 rows of a table where the column started with 'W'. You'd think after I typed that, I'd figure out the solution. That's not the case. Instead, I struggled. Here's what I originally came up with:

Dim q = From t In db.Employees Where t.LastName Like prefixText & "%" Select t.LastName Take count

Little did I realize that the Like operator in my query was the VB Like operator. So, I had to modify my lambda expression to this:

Dim q = From t In db.Employees Where t.LastName.StartsWith(prefixText) Select t.LastName Take count

You'll notice that StartsWith makes more sense. <sigh> If only VB were...

Published Wednesday, May 21, 2008 11:37 AM by Jason N. Gaylord
Filed under: ,

Comments

No Comments