2012-08-28 149 views
-4

我有這樣的數據結構:搜索嵌套列表<T>

List<string> sublist = new List<string>(); 
sublist.Add("university"); 
sublist.Add("organization"); 

List<List<string>> list = new List<List<string>>(); 
list.Add(sublist); 

然後:

Conference c = new Conference(); 
c.Orgs = list; 

我有會議對象的集合:

在此集合
class Conference 
{ 
    private List<List<string>>_orgs; 
    public List<List<string>> Orgs 
    { 
     set { _orgs = value; } get { return _orgs; } 
    } 
} 

數據

List<Conference> listConferences = new List<Conference>(); 
listConferences.Add(c); 

我想搜索像"uni"這樣的字符串,並找到會議的集合有像"uni"組織。我怎樣才能做到這一點?

+4

也許你應該花一些努力讓代碼在發佈問題之前實際編譯。 – leppie

+0

你確定你已經選擇了正確的數據結構來使用嗎?這看起來沒有什麼意義的IMO。 –

+0

我有這樣的XML文件:'<有機碼 「一」>大學<有機碼 「B」> 123987Bn<有機碼 「一」>組織<有機碼 「B」> 465465465sf'我選擇從XML文件保存這個數據的結構比較簡單。文件名字和數字。你對結構的建議是什麼? – TAIT

回答

0

你可以這樣做:

var selection = listConferences 
       .Where(x => x.Orgs.SelectMany(y => y).Any(y => y.Contains("uni"))) 
       .ToList(); 

注:

ToList()根據您的需求(例如,如果你選擇重複一次,你可以跳過)可能沒有必要。

+0

謝謝你。但是這個代碼在搜索例如「大學」時返回結果。我想用類似條件搜索 – TAIT

+0

@TAIT:是的,我的錯對不起。我忘了一段代碼。檢查我的編輯;) – digEmAll

+0

真的很棒! :) 非常感謝。在更短的時間內搜索400,000條記錄並獲得答案 – TAIT

0

請使用下面的代碼; 而不是第三個,您可以使用自己的會議列表。你現在可以使用類似於like的關鍵字。

List<string> first = new List<string>(); 
      first.Add("University"); 
      first.Add("Standard"); 

      List<List<string>> second = new List<List<string>>(); 
      second.Add(first); 

      List<List<List<string>>> third = new List<List<List<string>>>(); 
      third.Add(second); 

      var e = third.Find(delegate(List<List<string>> r) 
         { 
          bool isValid = false; 
          if(r.Count > 0) 
          { 
           foreach(List<string> s in r) 
           { 
            if(s.Count > 0) 
            { 
             isValid = s.FindAll(delegate(string t){ return t.StartsWith("uni", StringComparison.OrdinalIgnoreCase);}).Count > 0; 
            } 
           } 
          } 
          return isValid; 
         }); 
+0

謝謝你的回答 – TAIT

0

完成,使用LINQ的一個更多的鍛鍊。你應該對此感到舒服:

var univ = from p in c.Orgs 
         select p.FindAll(r => r.FindAll(s => s.StartsWith("univ", StringComparison.OrdinalIgnoreCase))); 
+0

謝謝你的回答 – TAIT