adsense

Wednesday, November 3, 2010

Using SqlMethods.Like method

The System.Data.Linq.SqlClient namespace includes a helper class called SqlMethods, which can includes a method called Like, that can be used in a Linq to SQL query:

var query = from c in ctx.Customers
            where SqlMethods.Like(c.City, "L_n%")
            select c;

This method gets the string expression to check (the customer's city in this example) and the patterns to test against which is provided in the same way you'd write a LIKE clause in SQL.

Using the above query generated the required SQL statement:

SELECT CustomerID, CompanyName, ...
FROM    dbo.Customers
WHERE  City LIKE [L_n%]

Cheers,
Happy Coding
Samitha

No comments:

Post a Comment