I have a collection class which inherits from BindingList and I can use the Sum function over it such as myList.Sum(x=>x.Quantity).
But when I implement the interface IEnumerable<SqlDataRecord> I can’t use it anymore, x.Quantity is not given as an option. How can I resolve this issue?
class item
{
public decimal Quantity { get; set; }
}
class items : BindingList<T>
{
}
items newItems = new items();
items.Sum(x=>x.Quantity);
The above code works but when I add the following code it doesn’t work anymore. It says the class doesn’t have a definition of Sum. What am I doing wrong?
class items : BindingList<T>, IEnumerable<SqlDataRecord>
{
}
items.Sum(x=>x.?);
When your class only inherited
BindingList<T>,myList.Sum(...)was unambiguously callingEnumerable<T>.Sum(myList, ...)viaBindingList<T>‘s inheritance fromCollection<T>.Now you have made your class directly implement
IEnumerable<SqlDataRecord>, it’s no longer clear which type ofEnumerable<?>to callSumon – is?meant to beTfrom yourBindingListinheritance orSqlDataRecordfrom your direct implementation?(For some reason, my VS reports this as “does not contain a definition…” rather than as an ambiguous call.)
Thus if you want to make your call as before you have to explicitly call
myList.Sum<T>(...).