如何使用扩展方法 Sum() 获取短裤列表的总和?

发布于 2024-09-12 15:14:45 字数 422 浏览 9 评论 0原文

我试图做这样的事情 -

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

我收到这个编译错误 -

“System.Collections.Generic.List”不包含“Sum”的定义,并且最佳扩展方法重载“System.Linq.Queryable.Sum(System.Linq.IQueryable)”具有一些无效参数

任何人都可以建议我如何才能使用扩展方法来计算短路之和?由于某种原因,扩展方法不支持它......

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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄居人 2024-09-19 15:14:45
int s = listofshorts.Sum(d => d);
int s = listofshorts.Sum(d => d);
囍孤女 2024-09-19 15:14:45

您可以为该方法提供 lambda:

List<short> listofshorts= new List<short>(); 
int s = listofshorts.Sum(a => (int)a);

You can provide the lambda for the method:

List<short> listofshorts= new List<short>(); 
int s = listofshorts.Sum(a => (int)a);
仙女山的月亮 2024-09-19 15:14:45
// This throws an InvalidCastException in .NET 3.5 SP1+
// DO NOT USE THIS CODE
listOfShorts.Cast<int>().Sum();

为了后代的利益,并指出这个看似明显的解决方案不起作用 - 我将在这个答案中留下有关 .NET 3.5 SP1+ 行为的以下链接:

// 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:

入画浅相思 2024-09-19 15:14:45

你可以做

int s = listofshorts.Aggregate((i1,i2) => i1+i2); 

You could do

int s = listofshorts.Aggregate((i1,i2) => i1+i2); 
能怎样 2024-09-19 15:14:45

就像其他人所建议的那样,您需要将短对象转换为 Enumerable.Sum 方法。不幸的是,对于某些类型(如 ulong 等)没有重载的 Sum 方法。

如果您经常需要它,我建议您自己编写一个扩展方法,这是我不久前为 ulong 和 ulong 所做的一个扩展方法?,您可以做一些非常类似的简短操作或您需要的任何其他类型:

    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;
    }

PS,在我纯粹出于好奇而查看了反射器之后,我的实现基于 Enumerable.Sum 实现:-P

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

或十年 2024-09-19 15:14:45

我可能会选择 .ForEach() 扩展名,这里不需要任何转换

short sum = 0;
myListOfShort.ForEach(val => sum += val)

I would probably chose the .ForEach() extension, you don't need any casting here

short sum = 0;
myListOfShort.ForEach(val => sum += val)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文