c#list查找重复更新多个列表

时间:2017-12-11 04:05:00

标签: c# asp.net list

我有3个列表lstTicketTypelstQuantity lstAmount。我正在尝试合并故障单类型并相应地更新其他两个列表

数据如下

lstTicketType | lstQuantity | lstAmount
---------------------------------------
Standard      |     1       |   8:5
Standard      |     1       |   8.5
Student       |     1       |   6.5
Student       |     1       |   6.5
Senior        |     1       |    4

我可以使用

找到重复项
      var query = lstTicketType.GroupBy(x => x)
      .Where(g => g.Count() > 1)
      .Select(y => new { Counter = y.Count() })
      .ToList();

如何更新列表以使其看起来像

lstTicketType | lstQuantity | lstAmount
---------------------------------------
Standard      |     2       |    17
Student       |     2       |    13
Senior        |     1       |    4

3 个答案:

答案 0 :(得分:3)

尝试使用" Sum"。不要依赖我的答案,并尝试编写自己的代码来了解它的工作原理。否则,你永远不会知道如何编码。这是我的参考 - https://stackoverflow.com/a/16522841/1554116

internal class Ticket
{
    public string TicketType { get; set; }
    public int Quantity { get; set; }
    public decimal Amount { get; set; }
}
internal class Program
{
    private static void Main(string[] args)
    {
        var list = new List<Ticket>();
        list.Add(new Ticket() { TicketType = "Standard", Quantity = 1, Amount = 8.5m });
        list.Add(new Ticket() { TicketType = "Standard", Quantity = 1, Amount = 8.5m });
        list.Add(new Ticket() { TicketType = "Student", Quantity = 1, Amount = 6.5m });
        list.Add(new Ticket() { TicketType = "Student", Quantity = 1, Amount = 6.5m });
        list.Add(new Ticket() { TicketType = "Senior", Quantity = 1, Amount = 4m });

        foreach (var item in list)
        {
            System.Console.WriteLine($"TicketType: {item.TicketType}, Quantity: {item.Quantity}, Amount: {item.Amount}");
        }

        System.Console.WriteLine("------------------------------------------");

        list = list.GroupBy(x => x.TicketType).Select(x => new Ticket()
        {
            TicketType = x.First().TicketType,
            Quantity = x.Sum(y => y.Quantity),
            Amount = x.Sum(y => y.Amount)
        }).ToList();

        foreach (var item in list)
        {
            System.Console.WriteLine($"TicketType: {item.TicketType}, Quantity: {item.Quantity}, Amount: {item.Amount}");
        }

        System.Console.Read();
    }
 }

答案 1 :(得分:0)

List<Ticket> tickets = new List<Ticket>()
            {
                new Ticket(){
                    TicketType="Standard",
                    Quantity =1,
                    Amout= 8.5M
                },
                new Ticket(){
                    TicketType="Standard",
                    Quantity =1,
                    Amout= 8.5M
                },
                new Ticket(){
                    TicketType="Student",
                    Quantity =1,
                    Amout= 6.5M
                },
                new Ticket(){
                    TicketType="Student",
                    Quantity =1,
                    Amout= 6.5M
                },
                new Ticket(){
                    TicketType="Senior",
                    Quantity =1,
                    Amout=4M
                },
            };

            var groupTicket = tickets.GroupBy(p => p.TicketType).Select(t => new
            {
                TicketType = t.Key,
                Quantity = t.Sum(x => x.Quantity),
                Amount = t.Sum(x => x.Amout)
            }).ToList();

答案 2 :(得分:0)

您的答案是代码

List<string> lstTicketType = new List<string>() { "Standard", "Standard", "Student", "Student", "Senior" };
        List<int> lstQuantity = new List<int>() { 1,1,1,1,1};
        List<double> lstAmount = new List<double>() {8.5 , 8.5 , 6.5 , 6.5 ,4};

        List<mergeClass> LstMC = new List<mergeClass>();

        for (int i = 0; i < lstTicketType.Count(); i++)
        {
            mergeClass NewMC = new mergeClass(lstTicketType[i], lstQuantity[i], lstAmount[i]);
            LstMC.Add(NewMC);
        }
        var query=LstMC.GroupBy(q=>q.TicketType)
                    .Select(q=>new{TicketType=q.First().TicketType,
                                    Quantity=q.Sum(a=>a.Quantity),
                                    Amount=q.Sum(a=>a.Amount)
                    }).ToList();

并且您必须在项目的下创建

 public class mergeClass
{
    public string TicketType { set; get; }
    public int Quantity { set; get; }
    public double Amount { set; get; }
    public mergeClass(string T, int Q, double A)
    {
        TicketType = T;
        Quantity = Q;
        Amount = A;
    }

}