2013-03-05 193 views
4

有没有办法让我按组过滤员工人数: 例如:不适用于LINQ?

List<String> notInGroups = GetNotInGroups(); 

    var list = from p in employees 
    where p.Group.Name notin(notInGroups) 
    select p; 

是有一些办法做这样的事情?

感谢

+0

哦,通常我会使用'Where(p =>!notInGroups.Contains(p.Group.Name))'(不确定这种类似于SQL的语法),但我会认真考虑使用'HashSet '而不是'列表'快速查找。 – 2013-03-05 14:25:55

回答

7

你可以包含,如:!

var list = from p in employees 
where !notInGroups.Contains(p.Group.Name) 
select p; 
+0

完美谢谢! – jmasterx 2013-03-05 14:26:01

2

无法测试,但不会像这样的工作?

var notInGroups = GetNotInGroups(); 
var list = from p in employees 
      where notInGroups.Contains(p.Group.Name) == false 
      select p; 
1

尝试where !notInGroups.Contains(p.Group.Name);为您WHERE条款。

1

试试这个:

var result = list1.Except(list2); 
+0

由于类型不匹配,这不起作用;他有一组员工和另一个组名。 – Servy 2013-03-05 14:31:56

1

你可以做这样的事情..

List<String> notInGroups = GetNotInGroups(); 

var list = from p in employees 
      where !(notInGroups.Contains(p.Group.Name)) 
      select p; 
+1

编辑:你忘了最后的大括号。 – Abbas 2013-03-05 14:31:19

1

List不是特别适合于通过收集搜索的任务,看它是否包含一个特定的项目,这正是你想要做的。在写代码是很容易的(目前已经有很多答案显示如何),你将显着使用更合适的数据结构,可以更有效地受益地搜索,如HashSet

var notInGroups = new HashSet<string>(GetNotInGroups()); 

var list = from p in employees 
    where !notInGroups.Contains(p.Group.Name) 
    select p; 
相关问题