我有以下列表
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; }
}
我想要一个结果
Tariff-1 -> Name - "blabla", Prices = {
Price1 = {Price = 343, Quantity = 3},
Price2 = {Price = 900, Quantity = 8} }
关税第一价格1 343 为 100 , 120 , 123 为3(数量) )月。
这是我不成功的尝试
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));
}
}
答案 0 :(得分:3)
不确定什么是名字blabla,但这是你可以获得价格的部分
var prices = Services
.SelectMany(arg => arg.Prices)
.GroupBy(arg => arg.Quantity)
.Select(arg => new { Price = arg.Sum(x => x.Price), Quantity = arg.Key })
.ToList();