獲取值的總數我有一個Class
:LINQ - 從嵌套列表
public class Company
{
public int id { get; set; }
public string title { get; set; }
public string address { get; set; }
public string city { get; set; }
public string zip { get; set; }
public List<string> phones { get; set; }
public List<string> categories { get; set; }
}
,我有一個Generic List
其中包含Class
:
public List<Company> Companies = new List<Company>();
我想要做的兩件事:
- 得到一個明確的類別列表
- 每 類別
得到公司的總數,我想我管理的第一件事:
Companies.SelectMany(c => c.categories).Distinct()
請告訴我,如果你認爲有什麼不妥。
我嘗試了第二步: Companies.SelectMany(c => c.categories).Where(c=>c == Category).Count()
但我不確定這是否是正確的。
一家公司可以有更多的1個類別,這是我需要的數量,有多少公司在特定類別下 – Dementic
@Demetic:看我的編輯 – SLaks
它仍然給我與我在哈桑的評論上寫的相同的結果。 – Dementic