c#将linq结果组合成一个特定类的新列表

时间:2017-08-21 12:18:01

标签: c#

我如何编写以迭代这三个初始列表中的每一个的结果,以便返回单个List。标题是分组值。

var invoiced = new List<Anonim> 
{ 
new Anonim {Category = 1, Title = "Legal", Amount = 20},
new Anonim {Category = 2, Title = "Accounting", Amount = 10}
}
var settled = new List<Anonim> {
new Anonim {Category = 1, Title = "Legal", Amount = 10}
}
var credit = new List<Anonim> {
new Anonim {Category = 1, Title = "Legal", Amount = 30},
new Anonim {Category = 2, Title = "Accounting", Amount = 20}
}


var result = new List<Result> {
new Result {Title = credit.Title, Invoiced = invoiced.Amount, Settled = settled.Amount, SumAmount = credit.Amount + settled.Amount + invoiced.Amount },
new Result {Title = credit.Title, Invoiced = invoiced.Amount, Settled = settled.Amount, SumAmount = credit.Amount + settled.Amount + invoiced.Amount }
}


public class Result
{
    public string Title { get; set; }
    public decimal Credit { get; set; }
    public decimal Invoiced { get; set; }
    public decimal Settled { get; set; }
    public decimal SumAmount { get; set; }
}

public class Anonim { 
    public int Category { get; set; }
    public string Title { get; set; }
    public decimal Amount { get; set; }
}

SumAmount是每个项目的已开票,结算,信用的总和

4 个答案:

答案 0 :(得分:3)

有点不清楚你想要发生什么,但假设你想按Title属性进行分组,这是一种方法。首先,将每个列表投影到Result类中,确保为每个列表设置相关属性,将它们组合成一个大列表,然后将它们分组以获得总计:

var groupedResults = invoiced.Select(i => new Result
{
    Title = i.Title,
    Invoiced = i.Amount
}).Union(settled.Select(i => new Result
{
    Title = i.Title,
    Settled = i.Amount
})).Union(credit.Select(i => new Result
{
    Title = i.Title,
    Credit = i.Amount
}));

var result = groupedResults
    .GroupBy(r => r.Title)
    .Select(g => new Result
    {
        Title = g.Key,
        Invoiced = g.Sum(r => r.Invoiced),
        Settled = g.Sum(r => r.Settled),
        Credit = g.Sum(r => r.Credit),
        SumAmount = g.Sum(r => r.Invoiced+r.Settled+r.Credit)
    });

答案 1 :(得分:0)

如果我理解正确,这是你想要的,但是由于大量的集合遍历,这是低效的。也许@DavidG解决方案会更高效。

var groupedSum = invoiced.Union(settled).Union(credit).GroupBy(g => g.Title).Select(g => new Result
{ 
    Title = g.Key,
    Credit = credit.Where(c => c.Title == g.Key).Sum(c => c.Amount),
    Settled = settled.Where(c => c.Title == g.Key).Sum(c => c.Amount),
    Invoiced = invoiced.Where(c => c.Title == g.Key).Sum(c => c.Amount),
    SumAmount = g.Sum(i => i.Amount)
});

答案 2 :(得分:0)

这是我能找到的最简单的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication73
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Anonim> invoiced = new List<Anonim> 
            { 
                new Anonim {Category = 1, Title = "Legal", Amount = 20},
                new Anonim {Category = 2, Title = "Accounting", Amount = 10}
            };

            List<Anonim> settled = new List<Anonim> {
                new Anonim {Category = 1, Title = "Legal", Amount = 10}
            };

            List<Anonim> credit = new List<Anonim> {
                new Anonim {Category = 1, Title = "Legal", Amount = 30},
                new Anonim {Category = 2, Title = "Accounting", Amount = 20}
            };

            List<Result> results = new List<Result>();


            List<string> titles = invoiced.Select(x => x.Title).ToList();
            titles.AddRange(settled.Select(x => x.Title).ToList());
            titles.AddRange(credit.Select(x => x.Title).ToList();
            titles = titles.Distinct().ToList();


            foreach(string title in titles)
            {
                Result newResult = new Result();
                results.Add(new Result);

                newResult.Title = title;
                newResult.Credit = credit.Where(x => x.Title == title).Sum(x => x.Amount);
                newResult.Invoiced = invoiced.Where(x => x.Title == title).Sum(x => x.Amount);
                newResult.Settled = settled.Where(x => x.Title == title).Sum(x => x.Amount);
                newResult.SumAmount = newResult.Credit + newResult.Invoiced + newResult.Settled;
            }

        }

    }
    public class Result
    {
        public string Title { get; set; }
        public decimal Credit { get; set; }
        public decimal Invoiced { get; set; }
        public decimal Settled { get; set; }
        public decimal SumAmount { get; set; }
    }

    public class Anonim
    {
        public int Category { get; set; }
        public string Title { get; set; }
        public decimal Amount { get; set; }
    }

}

答案 3 :(得分:0)

  var invoiced = new List<Anonim> 
                { 
                new Anonim {Category = 1, Title = "Legal", Amount = 20},
                new Anonim {Category = 2, Title = "Accounting", Amount = 10}
                };
            var settled = new List<Anonim> {
                new Anonim {Category = 1, Title = "Legal", Amount = 10}
                };
            List<Anonim> credit = new List<Anonim> {
                new Anonim {Category = 1, Title = "Legal", Amount = 30},
                new Anonim {Category = 2, Title = "Accounting", Amount = 20}
                };

            List<Result> x = new List<Result>();
            x.AddRange(invoiced.Select(y => new Result { Title = y.Title, Invoiced = y.Amount }));
            x.AddRange(settled.Select(y => new Result { Title = y.Title, Invoiced = y.Amount }));
            x.AddRange(credit.Select(y => new Result { Title = y.Title, Invoiced = y.Amount }));

var FinalList = x.GroupBy(a =&gt; a.Title).Select(Fn =&gt; new Result {             Title = Fn.Key,             Invoiced = Fn.Sum(a =&gt; a.Invoiced),``             已结算= Fn.Sum(a =&gt; a.Settled),             Credit = Fn.Sum(a =&gt; a.Credit),             SumAmount = Fn.Sum(a =&gt; a.Invoiced + a.Settled + a.Credit)     });