Hello,
The goal is to
Sum of articles, output in double or decimal.
Sum as or for int works well.
Not work!
double test = liste.Sum(getWarenwertDouble);
Trace.WriteLine($"Warenwert-----------{liste.Sum(getWarenwertDouble)}");
Picture try
// This works well too, because is only double, not an object.
List<double> Amount = new List<double>() { 1.0, 2.0, 3.0, 4.0 };
Console.WriteLine(Amount.Sum()); //10
var top3 = Amount.OrderByDescending(x => x).Take(3); // 4.0, 3.0, 2.0
public class Artikel
{
public string Bezeichnung { set; get; }
public decimal Preis { set; get; }
public int Menge { set; get; }
public override string ToString()
{
return Bezeichnung + "|" + Preis + "|" + Menge;
}
}
private void btnPredicateActionFunc_Click(object sender, EventArgs e)
{
List<Artikel> liste = new List<Artikel>();
liste.Add(new Artikel() { Bezeichnung = "short", Menge = 10, Preis = 99.99m });
liste.Add(new Artikel() { Bezeichnung = "shirt", Menge = 15, Preis = 78.99m });
liste.ForEach(getAction);
Trace.WriteLine("------------");
liste.ForEach(obj => Trace.WriteLine(obj));
Trace.WriteLine("------------");
liste.FindAll(getPredicate).ForEach(getAction);
Trace.WriteLine("------------");
Trace.WriteLine($"Warenwert-----------{liste.Sum(getWarenwert)}");
double test = liste.Sum(getWarenwertDouble);
Trace.WriteLine($"Warenwert-----------{liste.Sum(getWarenwertDouble)}");
}
public void getAction(Artikel obj)
{
Trace.WriteLine(obj);
}
public bool getPredicate(Artikel obj)
{
return obj.Preis < 90;
}
public int getWarenwert(Artikel obj)
{
return Convert.ToInt32(obj.Preis) * obj.Menge;
}
public double getWarenwertDouble(Artikel obj)
{
return Convert.ToDouble(obj.Preis) * obj.Menge;
}