1

I have a list of EQModel.

public class EQModel
    {
        public string IncEpicentre { get; set; }
        public decimal MagnitudeInMl { get; set; }
        public int TotalHits { get; set; }        
    }

I would like to group this list for different locations and also by different ranges of MagnitudesInMl value.

Ranges of : { 5, 6,7,8 }

I am able to group them separately using LINQ but am unable to group using both IncEpicenter and MagnitudeInMl.

For grouping by range, I used:

var ranges = new[] { 5, 6,7,8 };

var lsGrouped = lsEQData.GroupBy(x => ranges.FirstOrDefault(r => r > x.MagnitudeInMl))
                                .Select(g => new { g.Key,  TotalHits = g.Count().ToString()});

For IncEpicenter,

 var lsCount = lsEQData.GroupBy(x => x.IncEpicentre)
                          .Select(g => new { g.Key, TotalHits = g.Count().ToString() });

Now, I would like to group the list by both.

Any help would be highly appreciated.

pso
  • 819
  • 12
  • 25
  • Please [edit] your question to include the LINQ code you're using to group separately. Thanks for improving the question's reference value and making it more answerable! – Nathan Tuggy May 31 '15 at 00:54
  • Thanks @Nathan, I have made some edits here. – pso May 31 '15 at 01:11

1 Answers1

1

You can always group by a key made up of both your conditions. An anonymous class can be used for this, like so:

var groupedByBothConditions = lsEQData.GroupBy(x => new
{
    Range = ranges.FirstOrDefault(r => r > x.MagnitudeInMl),
    IncEpicentre = x.IncEpicentre
})
.Select(g => new { g.Key,  TotalHits = g.Count().ToString()});
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77