Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a LIKE query with linq? [duplicate]

Tags:

c#

linq-to-sql

How can i perform an LIKE query within Linq?

I have the following query i would like to execute.

var results = from c in db.costumers               where c.FullName LIKE "%"+FirstName+"%,"+LastName               select c; 
like image 860
mweber Avatar asked Oct 12 '10 10:10

mweber


People also ask

How do I find duplicate records in LINQ?

To find the duplicate values only:var duplicates = list. GroupBy(x => x. Key). Where(g => g.

How do like in LINQ?

In LINQ to SQL, we don't have a LIKE operator, but by using contains(), startswith(), and endswith() methods, we can implement LIKE operator functionality in LINQ to SQL.

Why LINQ is faster than SQL?

The popular answer is that LINQ is INtegrated with C# (or VB), thereby eliminating the impedance mismatch between programming languages and databases, as well as providing a single querying interface for a multitude of data sources.


1 Answers

You could use SqlMethods.Like(matchExpression,pattern)

var results = from c in db.costumers               where SqlMethods.Like(c.FullName, "%"+FirstName+"%,"+LastName)               select c; 

The use of this method outside of LINQ to SQL will always throw a NotSupportedException exception.

like image 110
Julien Hoarau Avatar answered Sep 21 '22 10:09

Julien Hoarau