I have an object model that contains a list of objects and these objects have a property that’s defined as a short. It looks somewhat like this:
public class MyObject
{
public short TheNumber { get; set;}
}
List<MyObject> TheListOfMyObjects = SomeMethodThatReturnsList();
Now I want to do a sum on TheNumber of all the objects in the list so I’m writing this:
var TheSum = (from a in TheListOfMyObjects
where....
select a.TheNumber).Sum();
I get an error saying that there’s no definion for Sum(). Why is this not working and how do I fix it?
Edit:
I have this that works:
var TheCount = (from a in TheListOfMyObjects
where....
select a).Count();
So yes, I have System.Linq in the using statement. Not sure why the downvotes.
Sum doesn’t have an overload for short (Int16), i.e. http://msdn.microsoft.com/en-us/library/system.linq.enumerable.sum.aspx
Either cast/parse first or do
.Sum(i => i)