How to use as in Linq Query?

How can I use as a query in LINQ .... in sql, for example ..

name like = 'apple';

thanks..

+7
linq
source share
4 answers

Use regular .NET methods. For example:

var query = from person in people where person.Name.StartsWith("apple") // equivalent to LIKE 'apple%' select person; 

(Or EndsWith or Contains .) LINQ to SQL will translate them into the appropriate SQL.

This will work in dotted notation too - there is nothing surprising in query expressions:

 // Will find New York var query = cities.Where(city => city.Name.EndsWith("York")); 
+11
source share

You need to use StartsWith , Contains or EndsWith depending on where your string might be displayed. For example:

 var query = from c in ctx.Customers where c.City.StartsWith("Lo") select c; 

will find all cities starting with "Lo" (for example, London).

 var query = from c in ctx.Customers where c.City.Contains("York") select c; 

will find all cities that contain "York" (for example, New York, Yorktown)

A source

+5
source share

name.contains ("apple");

0
source share

I use item.Contains ("criteria"), but it only works if you convert the bottom criteria and elements as follows:

  string criteria = txtSearchItemCriteria.Text.ToLower(); IEnumerable<Item> result = items.Where(x => x.Name.ToLower().Contains(criteria)); 
0
source share

All Articles