How to use "LIKE" in Linq to Object

I use Linq for Object, now how can I use something like LIKE used in sql query to filter data?

+3
source share
4 answers

Use a combination of Contains, StartsWithand EndsWith. These are all extension methods or methods on System.Stringand will work with LINQ. If this does not solve your problem, you will be forced to use regular expressions.

+3
source

try it

    string searchString = "NAME";
    List<string> lstNames = new List<string>()
                {"Name1"
                ,"Name2"
                ,"Name3"
                ,"Other names"
                ,"Something else"};

The following query will do the job

        var query1 = (from name in lstNames
                      where name.ToUpper().Contains(searchString)
                      select name);

        var query2 = lstNames.FindAll(name => name.ToUpper().Contains(searchString));

        var query3 = lstNames.Where(name => name.ToUpper().Contains(searchString));

Hope this helps

+2
source

IsMatch. % . * ___ ..

+1

. , StartsWith EndsWith . "LIKE" a% b ", .

0

Source: https://habr.com/ru/post/1735936/


All Articles