2011-11-04 37 views
0

我有這個字典mappings宣佈爲Dictionary<string, HashSet<string>>
我也有這個方法做的東西對一個HashSet在詞典:如何過濾使用LINQ的字典中的鍵列表

public void DoStuff(string key, int iClassId){ 
    foreach (var classEntry in 
      from c in mappings[key] 
      where c.StartsWith(iClassId + "(") 
      select c) 
    { 
     DoStuffWithEntry(classEntry); 
    } 
} 

private void DoStuffWithEntry(string classEntry){ 
    // Do stuff with classEntry here 
} 

在一種情況下,我需要做的這對一些在映射字典鍵,我想這是更好在密鑰列表上重寫和過濾,而不是爲每個密鑰調用DoStuff以優化執行。

目前,我這樣做:

DoStuff("key1", 123); 
DoStuff("key2", 123); 
DoStuff("key4", 123); 
DoStuff("key7", 123); 
DoStuff("key11", 123); 

邏輯上是這樣的,而不是調用DoStuff每個(FilterOnKeys不是一個方法 - 這正是我想...):

foreach (var classEntry in 
      from c in mappings.FilterOnKeys("key1", "key2", "key4", "key7", "key11") 
      where c.StartsWith(iClassId + "(") 
      select c) 
    { 
     DoStuffWithEntry(classEntry); 
    } 

回答

3

它聽起來像你想要的:

string[] keys = { "key1", "key2", ... } 
var query = from key in keys 
      from c in mappings[key] 
      ...; 

foreach (var entry in query) 
{ 
    ... 
} 

(我會親自使用一個單獨的變量的查詢只爲reada吳春明 - 我不是foreach環獲得巨大的聲明有點太敏銳)

+0

將這種方法比我原著的版本有更好的表現,或者是它只是一個「美容」簡化表達回事? – awe

+1

化妝品簡化真的。從根本上講,.NET字典不允許你一次查找多個密鑰。如果你正在使用LINQ to SQL或類似的東西,那麼可能會有更好的選擇。 –

0

你可以LINQ通過自己的方式映射 EDITED我錯過了一個嵌套的水平,他需要查詢hashsets不整部字典

public void DoStuff(IEnumerable<string> key, int iClassId) 
{ 
    mappings.Where(i=>key.Contains(i.Key)).ToList().ForEach(obj=> 
    { 
     foreach (var classEntry in 
      from c in obj.Value 
      where c.StartsWith(iClassId + "(") 
      select c) 
     { 
      DoStuffWithEntry(classEntry); 
     } 
} 

改變key參數和from c ...部分。

你這樣稱呼它

string[] keys = new string[]{"key1", "key2", ... , "keyN"}; 
DoStuff(keys, 123); 

這應該工作

+0

這會導致classEntry的值爲包含爲Dictionary的值的HashSet - 不循環HashSet的每個值。 – awe

+0

你是對的,我錯過了一步。我正在更新代碼 – Alex

0

你可以利用這樣的

var ids = {1, 2, 3}; 
var query = from item in context.items    
where ids.Contains(item.id)    
select item; 

你的情況

string[] keys = { "key1", "key2", ... } 
var query = from key in keys    
    where ids.Contains(keys )    
    select key ; 
0

我使用LINQ按照您的要求

var temp = eid.Select(i => 
      EmployeeList.ContainsKey(i) 
      ? EmployeeList[i] 
      : null 
     ).Where(i => i != null).ToList(); 

完整的C#源代碼是

public class Person 
{ 
    public int EmpID { get; set; } 
    public string Name { get; set; } 
    public string Department { get; set; } 
    public string Gender { get; set; } 
} 

void Main() 
{ 
    Dictionary<int, Person> EmployeeList = new Dictionary<int, Person>(); 
    EmployeeList.Add(1, new Person() {EmpID = 1, Name = "Peter", Department = "Development",Gender = "Male"}); 
    EmployeeList.Add(2, new Person() {EmpID = 2, Name = "Emma Watson", Department = "Development",Gender = "Female"}); 
    EmployeeList.Add(3, new Person() {EmpID = 3, Name = "Raj", Department = "Development",Gender = "Male"}); 
    EmployeeList.Add(4, new Person() {EmpID = 4, Name = "Kaliya", Department = "Development",Gender = "Male"}); 
    EmployeeList.Add(5, new Person() {EmpID = 5, Name = "Keerthi", Department = "Development",Gender = "Female"}); 

    List<int> eid = new List<int>() { 1,3 }; 

    List<Person> SelectedEmployeeList = new List<Person>(); 

    var temp = eid.Select(i => 
       EmployeeList.ContainsKey(i) 
       ? EmployeeList[i] 
       : null 
      ).Where(i => i != null).ToList(); 

}