Extension method, SumIf on generic List<T>
问题 I need to write an generic extension method for List(T) that conditionally considers each string property of T, then sums a corresponding decimal property of T if a condition is met. My effort thus far: // foreach(p in Persons) { if(p.Name == "mort"){sum p.Amount;} } public static double SumIf<T>(this T o, List<T> ListItems, string targetStr, ?strVals?, ?dblVals?) { double sum = 0; foreach(T item in ListItems) { if(item.?strVal? == targetStr){ sum += item.?dblVal? ; } } return sum; } Thanks