首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IGrouping故障

IGrouping故障
EN

Stack Overflow用户
提问于 2011-05-24 06:39:05
回答 1查看 339关注 0票数 1

我有以下清单

代码语言:javascript
复制
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>
                       {
                         new 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; }

    }

我想要一个结果

代码语言:javascript
复制
Tariff-1 -> Name - "blabla", Prices =  { 
            Price1 = {Price = 343, Quantity = 3}, 
            Price2 = {Price = 900, Quantity = 8}   }

关税第一价格Price1 343100120123,3个月(数量)。

这是我不成功的时间

代码语言:javascript
复制
 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));
                }

            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-24 06:49:03

不知道什么叫布拉贝拉,但这是你能得到价格部分的方法

代码语言:javascript
复制
var prices = Services
    .SelectMany(arg => arg.Prices)
    .GroupBy(arg => arg.Quantity)
    .Select(arg => new { Price = arg.Sum(x => x.Price), Quantity = arg.Key })
    .ToList();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6106623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档