次のリストを部門の値でグループ化する必要がありますが、LINQ 構文に問題があります。これが私のオブジェクトのリストです:
var people = new List<Person>
{
new Person { name = "John", department = new List<fields> {new fields { name = "department", value = "IT"}}},
new Person { name = "Sally", department = new List<fields> {new fields { name = "department", value = "IT"}}},
new Person { name = "Bob", department = new List<fields> {new fields { name = "department", value = "Finance"}}},
new Person { name = "Wanda", department = new List<fields> {new fields { name = "department", value = "Finance"}}},
};
私はグループ化をいじりました。これは私が持っている限りです:
var query = from p in people
from field in p.department
where field.name == "department"
group p by field.value into departments
select new
{
Department = departments.Key,
Name = departments
};
したがって、グループを反復処理できますが、人の名前をリストする方法がわかりません-
foreach (var department in query)
{
Console.WriteLine("Department: {0}", department.Department);
foreach (var foo in department.Department)
{
// ??
}
}
何を改善すべきか、または関連部門の名前をリストする方法についてのアイデアはありますか?