Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like condition in LINQ

Tags:

linq

sql-like

I am relatively new to LINQ and don't know how to do a Like condition. I have an IEnumerable list of myObject and want to do something like myObject.Description like 'Help%'. How can I accomplish this? Thanks

like image 391
MikeTWebb Avatar asked Dec 12 '22 19:12

MikeTWebb


1 Answers

Look here:

http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-like-operator.aspx

Snippet:

StartsWith and Contains:

var query = from c in ctx.Customers
            where c.City.StartsWith("L") && c.City.Contains("n")
            select c;

And if you should use it with LINQ to SQL (does not work with LINQ to Objects):

Custom LIKE (System.Data.Linq.SqlClient.SqlMethods.Like):

var query = from c in ctx.Customers
            where SqlMethods.Like(c.City, "L_n%")
            select c;
like image 119
Lasse Espeholt Avatar answered Mar 11 '23 04:03

Lasse Espeholt