EF LINQ / LAMDA。载有(列表[字符串])? [英] EF linq/lamda .contains(list[String])?

查看:189
本文介绍了EF LINQ / LAMDA。载有(列表[字符串])?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来评价,如果一个字符串包含一些列表的元素或列表中所有的元素?使用LINQ to实体?



我一直在尝试使用predicateBuilder和其他人,但即时通讯不能%100到放入系统。



修改



财产以后这样的:

 的String []字= searchString.Split(''); 

VAR resultado = db.users
。凡(U => u.fullName.contains(字))
。选择(S = gt;新建{USER_ID = S .id_user,农布雷= s.fullName})
.ToList();


解决方案

您需要扭转你的使用包含来检查集合为全名

 的String []字= searchString.Split(''); 

VAR resultado = db.users
。凡(U => words.Contains(u.fullName))
。选择(S = gt;新建{USER_ID = S .id_user,农布雷= s.fullName})
.ToList();

这将匹配的有一个词项阵列。



要匹配的所有在用户的全名的话 ,使用所有

  VAR resultado = db.users 
。凡(U => words.All(W => u.fullName.Contains(W))
。选择(S = gt;新建{USER_ID = s.id_user ,农布雷= s.fullName})
.ToList();


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

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

解决方案

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

这篇关于EF LINQ / LAMDA。载有(列表[字符串])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆