0

Is there a way for me to do like condition just like query in MVC using lambda? I currently have:

return repository.GetAll().Where(
            m => string.(m.Name, name, StringComparison.OrdinalIgnoreCase));

So how do I make this into a like condition?

Delimitry
  • 2,987
  • 4
  • 30
  • 39
user3731575
  • 121
  • 11
  • 1
    If you are using MS Sql Database with the [Collation](https://msdn.microsoft.com/en-us/library/ms180175.aspx) set to CI (Case Insensitive) you don't need to use the StringComparison enum. – Erik Philips Mar 13 '15 at 15:04

3 Answers3

0

use Contains() or StartsWith() or EndsWith() according to your requirement:

var filtered= data.Where(x=> x.Name.ToLower().Contains(name.ToLower()));  

in your code:

return repository.GetAll().Where(
        m => m.Name.ToLower().Contains(name.ToLower()));
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Usually you would use m.Name.Contains, that isn't possible here since it doesn't support case in-sensitiveness, like you have written down now. (if that is not intentional, use m.Name.Contains(name))

Another option mentioned here is the use of CompareInfo.IndexOf, but I don't know if it works well with EF.

Give this a try:

repository
.GetAll()
.Where
 ( m => CultureInfo.InvariantCulture.CompareInfo
       .IndexOf(m.Name, name, CompareOptions.IgnoreCase) > 0
 )
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

The only way I have managed this before is with raw sql syntax e.g.

context.Blogs.SqlQuery("SELECT * FROM SomeTable WHERE SomeTable.Name like {0}", name);
rdans
  • 2,179
  • 22
  • 32