LINQ To SQL LIKE Operator - Generating LIKE in SQL Server Statements
by David Hayden ( Florida .NET Developer ), Filed LINQ To SQL Tutorials
LINQ To SQL will generate the LIKE Operator in your WHERE clauses just like you would expect when you search for items in a table using StartsWith, EndsWith, and Contains in your LINQ To SQL Queries.
1. Example of LINQ To SQL Query using StartsWith.
View.Customers = from c in db.Customers
where c.ContactName.StartsWith("c")
orderby c.CompanyName
select c;
exec sp_executesql N'SELECT [t0].[CustomerID],...
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[ContactName] LIKE @p0
ORDER BY [t0].[CompanyName]',
'N'@p0 nvarchar(2)',@p0=N'c%'
2. Sample LINQ To SQL Query Using EndsWith.
View.Customers = from c in db.Customers
where c.ContactName.EndsWith("c")
orderby c.CompanyName
select c;
exec sp_executesql N'SELECT [t0].[CustomerID],...
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[ContactName] LIKE @p0
ORDER BY [t0].[CompanyName]',
'N'@p0 nvarchar(2)',@p0=N'%c'
3. LINQ To SQL Tutorial Using Contains:
View.Customers = from c in db.Customers
where c.ContactName.Contains("c")
orderby c.CompanyName
select c;
exec sp_executesql N'SELECT [t0].[CustomerID],...
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[ContactName] LIKE @p0
ORDER BY [t0].[CompanyName]',
'N'@p0 nvarchar(2)',@p0=N'%c%'
It make perfect sense that LIKE Operators would be generated in SQL Server using StartsWith, EndsWith, and Contains, but I was still pretty impressed that it all work as expected.
LINQ To SQL Tutorials
Source Feed: David Hayden ( Florida .NET Developer ), Filed LINQ To SQL Tutorials