Bu çözümlerden hangisi tercih edilir? Bir Liste için C# Linq ifadeleri veya toplam alt kümeler için foreach()
:List<ExampleInfo> exampleList = new List<ExampleInfo>();
public class ExampleInfo
{
internal ExampleInfo()
{ }
/* Business Properties */
public int Id { get; set; }
public string Type { get; set; }
public decimal Total { get; set; }
}
Ben 'Toplam' değerini kapalı tabanlı alt toplamları almak istiyoruz.
Seçenek 1:
var subtotal1 = exampleList.Where(x => x.Type == "Subtype1").Sum(x => x.Total);
var subtotal2 = exampleList.Where(x => x.Type == "Subtype2").Sum(x => x.Total);
Seçenek 2: Liste çoğu durumda < 10 ürün olacaktır
decimal subtotal1 = 0m;
decimal subtotal2 = 0m;
foreach (ExampleInfo example in exampleList)
{
switch (example.Type)
{
case "Subtype1":
subtotal1 += example.Total;
break;
case "Subtype2":
subtotal2 += example.Total;
break;
default:
break;
}
}
.
Düzelt: Chris çok iyi bir noktaya değindim, bahsetmedim. Program zaten .NET Framework 3.5 SP1 kullanıyor, dolayısıyla uyumluluk önemli bir konu değil.
Tam olarak ne yazıyordum aynı anda yazıyordum :) –
Sadece işlevsel eşdeğer yazdım –