如何在lambda中使用GroupBy?

时间:2012-11-20 18:42:34

标签: linq group-by

我正在尝试将以下代码转换为lambda样式,但没有成功。

DiscCurrentLocation[] old =
  (from v in volumeDC.Volumes
   join d in volumeDC.Disc_Vs
     on v.VolumeID equals d.DiscVolumeID
   group d by new { v.VolumeLibID, d.DiscCurrentLocation } into g
   where (g.Key.VolumeLibID == libraryId && g.Key.DiscCurrentLocation > -1
     && g.Count() > 1)
   select (DiscCurrentLocation)g.Key.DiscCurrentLocation
  ).ToArray<DiscCurrentLocation>();

有人可以告诉我如何转换它吗? 感谢

1 个答案:

答案 0 :(得分:3)

这应该是相同的:

DiscCurrentLocation[] old = volumeDC.Volumes
  .Join(volumeDC.Disc_Vs, (v) => v.VolumeID, (d) => d.DiscVolumeID,
      (v, d) => new { Volume = v, Disc_V = d })
  .GroupBy(vd => new { vd.Volume.VolumeLibID, vd.Disc_V.DiscCurrentLocation })
  .Where (grp => grp.Key.VolumeLibID == libraryId
      && grp.Key.DiscCurrentLocation > -1 && grp.Count() > 1)
  .Select (grp => (DiscCurrentLocation)grp.Key.DiscCurrentLocation)
  .ToArray<DiscCurrentLocation>()
;
相关问题