.net - How to use LINQ to group by multiple conditions in a C# List? -
this question has answer here:
- group multiple columns 9 answers
i have list of eqmodel.
public class eqmodel { public string incepicentre { get; set; } public decimal magnitudeinml { get; set; } public int totalhits { get; set; } } i group list different locations , different ranges of magnitudesinml value.
ranges of : { 5, 6,7,8 }
i able group them separately using linq unable group using both incepicenter , magnitudeinml.
for grouping range, 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, group list both.
any highly appreciated.
you can group key made of both conditions. anonymous class can used this, 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()});
Comments
Post a Comment