Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the sum of list of shorts using the extension method Sum()?

I was trying to do something like this -

List<short> listofshorts= new List<short>();
int s = listofshorts.Sum();
//this does not work...but same code works for a list of ints..

I got this compilation error -

'System.Collections.Generic.List' does not contain a definition for 'Sum' and the best extension method overload 'System.Linq.Queryable.Sum(System.Linq.IQueryable)' has some invalid arguments

Can anyone suggest how can I use an extension method to calculate the sum of shorts? For some reason the extension method does not support it ...

like image 358
Vishal Avatar asked Aug 03 '10 15:08

Vishal


5 Answers

int s = listofshorts.Sum(d => d);
like image 66
Giuseppe Accaputo Avatar answered Nov 23 '22 23:11

Giuseppe Accaputo


You can provide the lambda for the method:

List<short> listofshorts= new List<short>(); 
int s = listofshorts.Sum(a => (int)a);
like image 33
cjk Avatar answered Nov 23 '22 23:11

cjk


// This throws an InvalidCastException in .NET 3.5 SP1+
// DO NOT USE THIS CODE
listOfShorts.Cast<int>().Sum();

In the interest of posterity, and pointing out this seemingly obvious solution doesn't work - I'm going to leave this answer with the following links about .NET 3.5 SP1+ behavior:

  • Puzzling Enumerable.Cast InvalidCastException
  • http://blogs.msdn.com/b/dinesh.kulkarni/archive/2008/08/10/net-fx-3-5-sp1-two-perf-improvements-linq-to-objects-and-linq-to-sql.aspx
  • http://blogs.msdn.com/b/ed_maurer/archive/2008/02/16/breaking-change-in-linq-queries-using-explicitly-typed-range-variables.aspx
like image 43
3 revs Avatar answered Nov 24 '22 00:11

3 revs


You could do

int s = listofshorts.Aggregate((i1,i2) => i1+i2); 
like image 29
Jens Avatar answered Nov 24 '22 01:11

Jens


Like the others have suggested, you will need to cast the short objects to a type which is supported by the Enumerable.Sum method. Unfortunately there are no overloaded Sum method for some of the types like ulong, etc.

If you're gonna be needing it very often though, I'd recommend writing an extension method yourself, here's one I did a while back for ulong and ulong?, you can do something very similar for short or any other types you need:

    public static ulong Sum(this IEnumerable<ulong> source)
    {
        var sum = 0UL;

        foreach (var number in source)
        {
            sum += number;
        }

        return sum;
    }

    public static ulong? Sum(this IEnumerable<ulong?> source)
    {
        var sum = 0UL;

        foreach (var nullable in source)
        {
            if (nullable.HasValue)
            {
                sum += nullable.GetValueOrDefault();
            }                
        }

        return sum;
    }

P.S. my implementations are based on the Enumerable.Sum implementation after I took a peek with reflector purely out of curiosity :-P

like image 22
theburningmonk Avatar answered Nov 24 '22 01:11

theburningmonk