开发者

IGrouping trouble

开发者 https://www.devze.com 2023-03-07 19:05 出处:网络
I have the following list class Programm { public static void Main(string[] args) { List<Service> Services =new List<Service>

I have the following list

class Programm
    {

        public static void Main(string[] args)
        {
           List<Service> Services =new List<Service>
            {
                  new Service
                      {
                        Name = "name1",
                        Prices = new List<BEPrice>
                           {
                            new BEPrice
                              {
                               Price = 120,
                               Quantity = 3
                              } 

                            }
                      },
               new Service
                   {
                     Name = "name2",
                     Prices = new List<BEPrice>
                       {
                         开发者_如何学Pythonnew BEPrice
                           {
                            Price = 123,
                            Quantity = 3
                           } 

                     }
                },

              new Service
                {
                 Name = "name3",
                 Prices = new List<BEPrice>
                 {
                   new BEPrice
                    {
                      Price = 100,
                      Quantity = 3
                    } 

                  }
              },

              new Service
               {
                 Name = "name4",
                 Prices = new List<BEPrice>
                 {
                  new BEPrice
                   {
                    Price = 900,
                    Quantity = 8
                  } 

                 }
             }
         };






    }


    public class Tariff
    {

        public string Name { get; set; }
        public List<BEPrice> Prices { get; set; }
    }


    public class Service
    {

        public string Name { get; set; }
        public List<BEPrice> Prices { get; set; }
        public Tariff Tariff;
    }


    public class BEPrice
    {

        public decimal Price { get; set; }
        public int Quantity { get; set; }

    }

I want a result as

Tariff-1 -> Name - "blabla", Prices =  { 
            Price1 = {Price = 343, Quantity = 3}, 
            Price2 = {Price = 900, Quantity = 8}   }

The tariff first price Price1 343 is a sum of 100, 120, 123 for 3 (Quantity) month.

Here is my unsuccessful attemp

 foreach (var groupedPrices in Services.Select(s => s.Prices.GroupBy(p => p.Quantity)))
            {

                foreach (var p in groupedPrices.Select(x => x.Key))
                    Console.WriteLine(p);


                foreach (var price in groupedPrices)
                {

                    _prices.AddRange(price.Select(p => p));
                }

            }


Not sure what is name blabla, but this is how you can get prices part

var prices = Services
    .SelectMany(arg => arg.Prices)
    .GroupBy(arg => arg.Quantity)
    .Select(arg => new { Price = arg.Sum(x => x.Price), Quantity = arg.Key })
    .ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号