Group and amount with many C # choices?

I have an incoming array with a count per line and a line that represents one or more keys separated by an underscore.

for each key that I would like to group and summarize the total number, if the key appears in a line with a total number of 5, each element separated by an underscore will increase by 5.

I'm just wondering how this will be presented in linq ...

class Owl { public int SpeciesCount { get; set; } public string BandIdentifier { get; set; } } public class GoOwl { public GoOwl(Owl[] owls) { //just making a list of test data to illustrate what would be coming in on the array var owlList = new List<Owl>(); owlList.Add(new Owl { SpeciesCount = 2, BandIdentifier = "OWL1" }); owlList.Add(new Owl { SpeciesCount = 1, BandIdentifier = "OWL1_OWL2_OWL3" }); owlList.Add(new Owl { SpeciesCount = 2, BandIdentifier = "OWL3" }); owlList.Add(new Owl { SpeciesCount = 5, BandIdentifier = "OWL2_OWL3" }); //i'd ideally like to have something like a row for each band identifier split on underscore plus a total species count.. //where you'd sum the species count for each underscored item and group } } 

the following will be the desired output as separate Owl objects

 ["OWL1", 3] ["OWL2", 6] ["OWL3", 8] 

I still do not get SelectMany ..

Greetings

+4
source share
3 answers

In free sitnaks:

 //For each 'owlItem' in the owlList, select an anonymous objects for each key in the BandIdentifier string, keeping track of the associated SpeciesCount //Since each call to Split('_').Select(...) produces an IEnumerable of those anonymous objects, use SelectMany to flatten the IEnumerable to IEnumerables owlList.SelectMany(owlItem => owlItem.BandIdentifier.Split('_') .Select(key => new { OwlKey = key, owlItem.SpeciesCount })) //Group together those anonymous objects if they share the same key .GroupBy(info => info.OwlKey) //For each of the groups, sum together all the associated SpeciesCounts .Select(group => new { group.Key, SpeciesCount = group.Sum(info => info.SpeciesCount) })' 
+8
source

It looks like you want this:

 var results = owlList.SelectMany(owl => owl.BandIdentifier.Split('_'), (owl, band) => new { owl, band }) .GroupBy(x => x.band) .Select(group => new Owl { BandIdentifier = group.Key SpeciesCount = group.Sum(g => g.SpeciesCount) }); 

Or in the query syntax:

 var results = from owl in owlList from band in owl.BandIdentifier.Split('_') group owl by band into group select new Owl { BandIdentifier = group.Key SpeciesCount = group.Sum(g => g.SpeciesCount) }; 
+2
source
  var owlResults = owlList //Use SelectMany to split and select all species //and have a select to get the count along with the species .SelectMany(O => O.BandIdentifier.Split('_') .Select(owl => new { Species = owl, Count = O.SpeciesCount })) //Here you can group and get the sum .GroupBy(O => O.Species) .Select(owl => new { Species = owl.Key, Count = owl.Sum(o => o.Count) }); 
0
source

Source: https://habr.com/ru/post/1479547/


All Articles