Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do SQL Like % in Linq?

I have a procedure in SQL that I am trying to turn into Linq:

SELECT O.Id, O.Name as Organization FROM Organizations O JOIN OrganizationsHierarchy OH ON O.Id=OH.OrganizationsId where OH.Hierarchy like '%/12/%' 

The line I am most concerned with is:

where OH.Hierarchy like '%/12/%' 

I have a column that stores the hierarchy like /1/3/12/ for example so I just use %/12/% to search for it.

My question is, what is the Linq or .NET equivalent to using the percent sign?

like image 410
Matt Dell Avatar asked May 07 '09 16:05

Matt Dell


People also ask

Is LINQ similar to SQL?

The main difference between LINQ and SQL is that LINQ is a Microsoft . NET framework component, which adds native data querying capabilities to . NET languages, while SQL is a standard language to store and manage data in RDBMS.

What you can do with LINQ to SQL?

LINQ to SQL supports all the key capabilities you would expect as a SQL developer. You can query for information, and insert, update, and delete information from tables.

Which is faster SQL or LINQ?

Sql is faster than Linq. Its simple: if I m executing a sql query directly its a one way process whereas if I m using linq, first its been converted to sql query and then its executed.

How do I select a query in LINQ?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.


1 Answers

.Where(oh => oh.Hierarchy.Contains("/12/")) 

You can also use .StartsWith() or .EndsWith().

like image 154
andleer Avatar answered Oct 24 '22 12:10

andleer