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

允我心安 提交于 2019-12-05 19:05:22

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();

It's better to do with intersect

 IEnumerable<string> first = ...;
 IEnumerable<string> second= ...;
var duplicates = first.Intersect(second);

and doest contains

bool contains = duplicates.Any();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!