Question

Here's the scenario:

Given a List of Outputs each associated with an integer based GroupNumber. For each distinct GroupNumber within the List of Outputs starting with the lowest GroupNumber (1). Cycle through that distinct group number set and execute a validation method.

Basically, starting from the lowest to highest group number, validate a set of outputs first before validating a higher groupnumber set.

Thanks, Matt

Was it helpful?

Solution

There's almost too many ways to solve this:

Here's one for a void Validate method.

source
  .GroupBy(x => x.GroupNumber)
  .OrderBy(g => g.Key)
  .ToList()
  .ForEach(g => Validate(g));

Here's one for a bool Validate method.

var results = source
  .GroupBy(x => x.GroupNumber)
  .OrderBy(g => g.Key)
  .Select(g => new
  {
      GroupNumber = g.Key,
      Result = Validate(g),
      Items = g.ToList()
  })
  .ToList();

OTHER TIPS

If you need them as groups:

var qry = source.GroupBy(x=>x.GroupNumber).OrderBy(grp => grp.Key);
foreach(var grp in qry) {
    Console.WriteLine(grp.Key);
    foreach(var item in grp) {...}
}

If you just need them ordered as though they are grouped:

var qry = source.OrderBy(x=>x.GroupNumber);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top