Ben bir ürün koleksiyonuna sahipLinq: GroupBy, Sum ve Kont
Şimdi ürün koduna göre grubuna koleksiyonu istiyor ve her kod için isim, numara veya ürünleri içeren bir nesne döndürürpublic class Product {
public Product() { }
public string ProductCode {get; set;}
public decimal Price {get; set; }
public string Name {get; set;}
}
ve her ürün için toplam fiyat.
public class ResultLine{
public ResultLine() { }
public string ProductName {get; set;}
public string Price {get; set; }
public string Quantity {get; set;}
}
Yani o zaman toplamını hesaplamak ve ayrıca her ürün kodu için kayıtların sayısını saymak, ProductCode tarafından gruba GroupBy kullanın.
NedenseList<Product> Lines = LoadProducts();
List<ResultLine> result = Lines
.GroupBy(l => l.ProductCode)
.SelectMany(cl => cl.Select(
csLine => new ResultLine
{
ProductName =csLine.Name,
Quantity = cl.Count().ToString(),
Price = cl.Sum(c => c.Price).ToString(),
})).ToList<ResultLine>();
, toplamı doğru yapılır ancak sayım hep 1.
sampe veridir:
List<CartLine> Lines = new List<CartLine>();
Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
Lines.Add(new CartLine() { ProductCode = "p2", Price = 12M, Name = "Product2" });
Bu
Bugüne kadar ne varÖrnek veriler ile sonuç:
Product1: count 1 - Price:13 (2x6.5)
Product2: count 1 - Price:12 (1x12)
Ürün 1 sayısı = 2!
basit bir konsol uygulamasında bu taklit etmeye çalıştı ama aşağıdaki sonucu var:Product1: count 2 - Price:13 (2x6.5)
Product1: count 2 - Price:13 (2x6.5)
Product2: count 1 - Price:12 (1x12)
Ürün1: Sadece ... bir kere listelenmiş olmalıdır Yukarıdaki pastebin bulunabilir için kodu : http://pastebin.com/cNHTBSie
Tamam Benim konsol uygulaması çalışıyor. First() kullanmak ve SelectMany'i dışarıda bırakmak için teşekkürler. ResultLine aslında bir ViewModel'dir.Fiyat, para birimi işaretiyle biçimlendirilecek. Bu yüzden dizgiye ihtiyacım var. Ama miktarını int olarak değiştirebilirim .. Eğer web sitem için de yardımcı olabilirse şimdi göreceğim. Seni bilgilendirecegim. – ThdK
@ThdK: Hayır, 'Price'ı bir ondalık olarak tutmalı ve sonra nasıl biçimlendirdiğinizi değiştirmelisiniz. Veri temsilini temiz tutun ve sadece mümkün olan en son bir sunum görünümüne geçin. –
Neden ProductCode ve İsimle Gruplandırılmıyor? Bunun gibi bir şey: .GroupBy (l => new {l.ProductCode, l.Name}) ve ProductName kullanın = c.Key.Name, –