有没有办法让我按组过滤员工人数: 例如:不适用于LINQ?
List<String> notInGroups = GetNotInGroups();
var list = from p in employees
where p.Group.Name notin(notInGroups)
select p;
是有一些办法做这样的事情?
感谢
有没有办法让我按组过滤员工人数: 例如:不适用于LINQ?
List<String> notInGroups = GetNotInGroups();
var list = from p in employees
where p.Group.Name notin(notInGroups)
select p;
是有一些办法做这样的事情?
感谢
你可以包含,如:!
var list = from p in employees
where !notInGroups.Contains(p.Group.Name)
select p;
完美谢谢! – jmasterx 2013-03-05 14:26:01
无法测试,但不会像这样的工作?
var notInGroups = GetNotInGroups();
var list = from p in employees
where notInGroups.Contains(p.Group.Name) == false
select p;
尝试where !notInGroups.Contains(p.Group.Name);
为您WHERE
条款。
你可以做这样的事情..
List<String> notInGroups = GetNotInGroups();
var list = from p in employees
where !(notInGroups.Contains(p.Group.Name))
select p;
编辑:你忘了最后的大括号。 – Abbas 2013-03-05 14:31:19
List
不是特别适合于通过收集搜索的任务,看它是否包含一个特定的项目,这正是你想要做的。在写代码是很容易的(目前已经有很多答案显示如何),你将显着使用更合适的数据结构,可以更有效地受益地搜索,如HashSet
:
var notInGroups = new HashSet<string>(GetNotInGroups());
var list = from p in employees
where !notInGroups.Contains(p.Group.Name)
select p;
哦,通常我会使用'Where(p =>!notInGroups.Contains(p.Group.Name))'(不确定这种类似于SQL的语法),但我会认真考虑使用'HashSet'而不是'列表'快速查找。 –
2013-03-05 14:25:55