Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF linq/lambda .contains(list[String])?

is there any way to evaluate if a string contains some of the elements of a list or all the elements of the list? using linq to entities?

i have been trying to use predicateBuilder and others but im not %100 into thoses.

EDIT

something like:

string[] words = searchString.Split(' ');

var resultado = db.users
                        .Where(u => u.fullName.contains(words) )
                                .Select(s => new { user_id = s.id_user, nombre = s.fullName})
                                .ToList();
like image 594
EricGS Avatar asked Dec 12 '22 14:12

EricGS


1 Answers

You need to reverse your use of Contains to check the words collection for the fullName:

string[] words = searchString.Split(' ');

var resultado = db.users
    .Where(u => words.Contains(u.fullName))
    .Select(s => new { user_id = s.id_user, nombre = s.fullName})
    .ToList();

That will match one item in the words array.

To match all of words in a user's fullName, use All:

var resultado = db.users
    .Where(u => words.All(w => u.fullName.Contains(w))
    .Select(s => new { user_id = s.id_user, nombre = s.fullName})
    .ToList();
like image 176
mattytommo Avatar answered Dec 26 '22 23:12

mattytommo