8,494
社区成员




public class Product
{
public Product(string SUK, string SName, int ICount,decimal FTotalPrice)
{
this.SUK = SUK;
this.SName = SName;
this.ICount = ICount;
this.FTotalPrice = FTotalPrice;
}
public string SUK { get; set; }
public string SName { get; set; }
public Int32 ICount { get; set; }
public decimal FTotalPrice { get; set; }
}
List<Product> list = new List<Product>();
list.AddRange(new Product[]{
new Product("A01","红烧肉",3,60),
new Product("A01","红烧肉",-1,20),
new Product("A02","白切肉",2,40),
new Product("A03","豆腐汤",2,30),
new Product("A03","豆腐汤",-1,15),
});
void Main()
{
List<Product> list = new List<Product>();
list.AddRange(new Product[]{
new Product("A01","红烧肉",3,60),
new Product("A01","红烧肉",-1,20),
new Product("A02","白切肉",2,40),
new Product("A03","豆腐汤",2,30),
new Product("A03","豆腐汤",-1,15),
});
var query = from p in list
group p by new {p.SUK, p.SName} into g
let unitPrice = Math.Abs(g.First().FTotalPrice/g.First().ICount)
select new Product
{
SUK = g.Key.SUK,
SName = g.Key.SName,
ICount = g.Sum(x => x.ICount),
FTotalPrice = unitPrice * g.Sum(x => x.ICount)
};
}
public class Product
{
public Product()
{
}
public Product(string SUK, string SName, int ICount,decimal FTotalPrice)
{
this.SUK = SUK;
this.SName = SName;
this.ICount = ICount;
this.FTotalPrice = FTotalPrice;
}
public string SUK { get; set; }
public string SName { get; set; }
public Int32 ICount { get; set; }
public decimal FTotalPrice { get; set; }
}