私は製品のコレクションを持っています
public class Product {
   public Product() { }
   public string ProductCode {get; set;}
   public decimal Price {get; set; }
   public string Name {get; set;}
}
ここで、製品コードに基づいてコレクションをグループ化し、名前、各コードの製品数、および各製品の合計価格を含むオブジェクトを返したいと考えています。
public class ResultLine{
   public ResultLine() { }
   public string ProductName {get; set;}
   public string Price {get; set; }
   public string Quantity {get; set;}
}
そこで、GroupBy を使用して ProductCode でグループ化し、合計を計算して、各製品コードのレコード数もカウントします。
これは私がこれまでに持っているものです:
List<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>();
何らかの理由で、合計は正しく行われますが、カウントは常に 1 です。
サンプルデータ:
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" });
サンプルデータの結果:
Product1: count 1   - Price:13 (2x6.5)
Product2: count 1   - Price:12 (1x12)
商品 1 のカウント = 2 にする必要があります。
これを単純なコンソール アプリケーションでシミュレートしようとしましたが、次の結果が得られました。
Product1: count 2   - Price:13 (2x6.5)
Product1: count 2   - Price:13 (2x6.5)
Product2: count 1   - Price:12 (1x12)
Product1: 一度だけリストする必要があります... 上記のコードは、pastebin にあります: http://pastebin.com/cNHTBSie