How do we use LIKE (from SQL) in LINQ?
One option:
-> use string.Contains() method
string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };//get companies which have 'oo' in themIEnumerable<string> companiesWithAnO = (from c in companieswhere c.Contains("oo")select c).ToArray();
Also we can use string.StartsWith() and EndsWith().
-> Second option: This is only if we are using LINQ To SQL.
string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };//get companies which have 'oo' in themIEnumerable<string> companiesWithAnO = from c in companieswhere SqlMethods.Like(c,"%oo%")select c;
The IN operator is also quite easy.
string orangeFruit = "orange";string[] fruitBasket = {"apple","orange","mango","leechy" };//check orange is in the fruit basketbool orangeFound = fruitBasket.Contains(orangeFruit);
“Contains” is the extension method in the IEnumerable<T> class.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.