Khayralla
Khayralla

Reputation: 187

How to use GroupBy in lambda?

I am trying to convert the following code into lambda style but without success.

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>();

Can somebody show me how to convert it? Thanks

Upvotes: 0

Views: 75

Answers (1)

mellamokb
mellamokb

Reputation: 56779

This should be identical:

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>()
;

Upvotes: 3

Related Questions