有沒有辦法讓我按組過濾員工人數: 例如:不適用於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