Tuesday, November 03, 2009

SQL Like in LinqToSql

The more I learn about Linq and it's SQL Server specific variant LinqToSql the more I giggle with glee. I recently learned how functionality similar to SQL's Like clause is better expressed from linq to sql.

I have known about the use of SqlMethods.Like(...) for a while, but only recently realized a better way after reading another's code. Here's the SqlMethods.Like() way:
list.Where(o => SqlMethods.Like(o.Name, "%" + Name + "%"))
Here is that same statement more appropriately expressed:
list.Where(o => o.Name.Contains(Name))
Clever eh? Just like I'd bake it if I were using LinqToObject. Now I'm not constrained to the LinqToSql context. Good news continues with the string class' .StartsWith(...) and .EndsWith(...) methods working exactly as one might expect.

This also goes to show just how far the LinqToSql team went to blend into the Linq paradigm.

1 comment:

Unknown said...

you said giggle with glee. My day, made.