如何按列表合并并加总无键值

时间:2018-04-19 06:40:06

标签: c# .net

例如,我有一个按列表分组:

List<Cl> list = new List<Cl>()
        {
            new Cl() { vi = 1, vs = "q", am = 20 },
            new Cl() { vi = 1, vs = "w", am = 30 },
            new Cl() { vi = 2, vs = "w", am = 40 },
            new Cl() { vi = 1, vs = "q", am = 50 }
        };
groups = list.GroupBy(b => new { b.vi, b.vs }).ToList();

我希望通过汇总am字段得到合并列表:

Key       am
[1, "q"] [70] 
[1, "w"] [30] 
[2, "w"] [40]

是否可以在没有词典或KeyValuePair的情况下实现它?

我将不胜感激任何帮助。

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

class Key
{
    public int Vi { get; set; }
    public string Vs { get; set; }

    public Key(int vi, string vs)
    {
        Vi = vi;
        Vs = vs;
    }
}

var result = groups = list.GroupBy(b => new { b.vi, b.vs })
                          .Select(gr=> new { Key = Key(gr.vi,gr.vs)
                                           , Value = gr.Sum(x=>x.am)})
                          .ToList();

答案 1 :(得分:0)

首先ARRAY : ["e", "s", "l", "e"] ARRAY : ["s", "e", "l", "e"] ARRAY : ["l", "e", "s", "e"] ARRAY : ["e", "e", "s", "l"] => [["e", "s", "l", "e"], ["s", "e", "l", "e"], ["l", "e", "s", "e"], ["e", "e", "s", "l"]] ,然后GroupBy()提升每组的Sum()

am

答案 2 :(得分:0)

试试这样:

<div class="col-xs-12 col-md-6 example-col">
     <p>Custom defined format descriptions</p>
     <kendo-dateinput format="G"
       [formatPlaceholder]="{
         year: 'y', month: 'M', day: 'd',
         hour: 'h', minute: 'm', second: 's'
       }"
     ></kendo-dateinput>
</div>