Home > Software engineering >  SQL Like clause in LINQ
SQL Like clause in LINQ

Time:11-07

I am trying to migrate a SQL Server stored procedure to LINQ and I am having difficulties when I have a list of partial matches that I need to find in the main table.

In short I need to replicate the following SQL into a LINQ. Any help would be much appreciated.

SQL

DECLARE @filters TABLE([filter] NVARCHAR(20))

INSERT INTO @filters VALUES ('Den%');
INSERT INTO @filters VALUES ('%zil');
INSERT INTO @filters VALUES ('%la%');

SELECT c.*  
FROM [Northwind].[dbo].[Customers] c
INNER JOIN @filters f ON c.Country LIKE (f.filter)
ORDER BY Country

C#

var filters = new string[] { "Den*", "*zil", "*la*" };

var results = from C in ctx.Customers
              join f in filters c.Country like f
              Select new 
                     {
                         c.CustomerId,
                         c.Country
                     };

CodePudding user response:

Yo could use the following example:

var result = context.Customers.AsNoTracking()
                    .Include(x => x.Country)
                    .Where(x => x.Country.Contains("Den"));

CodePudding user response:

var result = context.Customers.AsNoTracking()
                    .Include(x => x.Country)
                    .Where(x => x.Country.Contains("la") || x.Country.Startwith("Den") || x.Country.EndWith("zil"))
  • Related