Linq逆向GroupBy

时间:2013-03-07 13:48:19

标签: c# linq lambda grouping

我有这个问题:

from e in Employee
join t in TimeTable
    on e.Id equals t.EmpId
group new {e,t} by e.Id into g
orderby g.Key
select new 
{
  Emp = g.Key, 
  Items = g.Select(c => c.t.OtherTable.Somevalue).Distinct()
}

它工作正常,但我需要撤消分组,以便将所有Employees分组到OtherTable.Somevalue

我该怎么做?

1 个答案:

答案 0 :(得分:2)

可能会喜欢这样,它会起作用

from e in Employee
join t in TimeTable
    on e.Id equals t.EmpId
group new {e,t} by t.OtherTable.Somevalue into g
orderby g.Key
select new 
{
  SomeValue = g.Key, 
  Emplyees = g.Select(c => c.e.Id).Distinct()
}