4

我有一个传入数组,每行计数和一个字符串,该字符串表示一个或多个键的下划线拆分。

对于每个键,我想对总数进行分组并求和,如果键连续出现总共 5 个,则由下划线分割的每个项目的总数增加 5。

我只是想知道这将如何在 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


    }
}

以下将是作为单个 Owl 对象的所需输出

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

我还没有完全得到 SelectMany..

干杯

4

3 回答 3

8

流利的语法:

//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) })'
于 2013-05-07T14:23:10.470 回答
3

看起来你想要这个:

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

或者在查询语法中:

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)
    };
于 2013-05-07T14:18:31.273 回答
0
 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) });
于 2013-05-07T15:01:47.060 回答