2013-10-23 70 views
1

我有一些類的列表對象類型列表對象選擇元素的單一屬性,基於其他的Hashset的元素

class person 
{ 
    public string id { get; set; } 
    public string regid { get; set; } 
    public string name { get; set; } 
} 

List<person> pr = new List<person>(); 
pr.Add(new person { id = "2",regid="2222", name = "rezoan" }); 
pr.Add(new person { id = "5",regid="5555", name = "marman" }); 
pr.Add(new person { id = "3",regid="3333", name = "prithibi" }); 

和字符串類型的HashSet的,

HashSet<string> inconsistantIDs = new HashSet<string>(); 
inconsistantIDs.Add("5"); 

現在我只想從pr列表中獲得所有* regid * s,該列表包含不一致ID的HashSet中的id,並將它們存儲到字符串類型的另一個HashSet中。

我已經嘗試過,但只能得到所有在ID不同的ID列表中的人(這只是一個例子)。

HashSet<person> persons = new HashSet<person>(
      pr.Select(p=>p).Where(p=> 
        inconsistantIDs.Contains(p.id) 
       )); 

任何人都可以幫助我嗎?

+2

什麼是你想要的結果?你說你想要「只有所有的* regid * s」,但那是一個字符串屬性,爲什麼'persons'是'HashSet '然後呢? –

+0

我想要的結果是僅基於不一致的ID下的ID從公共列表中獲取regids。例如,在這裏我想從pr列表中獲得基於id 5的regid ='5555'在不一致的ID @TimSchmelter – Rezoan

+0

這是我所能做的,但它不是我想要的輸出。它和示例人員HashSet @TimSchmelter。我的dsire輸出只是爲了得到regids而不是整個人 – Rezoan

回答

2
var regIDs = from p in pr join id in inconsistantIDs on p.id equals id 
      select p.regid; 
HashSet<string> matchingRegIDs = new HashSet<string>(regIDs); // contains: "5555" 
+0

正是我想要的感謝 – Rezoan

1

我不知道什麼是你想要的輸出,但我會盡量嘗試:

HashSet<string> persons = new HashSet<string>(
      pr.Select(p=>p.regid) 
       .Where(p=> inconsistantIDs.Any(i=>p.Contains(i)))); 
+0

也感謝你的努力。 – Rezoan

相關問題