如何在LINQ中的单个查询中使用不同的()和sum()[英] How to Use Distinct() and Sum() in Single Query in Linq

本文是小编为大家收集整理的关于如何在LINQ中的单个查询中使用不同的()和sum()的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在这里我有2列,例如 id,数量.我想在LINQ中获取独特的ID及其数量总和. 我尝试了这样的

var r = list.Where(some operation)
            .Select(x => x.Id)
            .Distinct()
            .Select(x => (int?)x.Quantity))
            .Sum();

在x. 请给出您的建议

推荐答案

如果我假设ID是int而数量是字符串,则您也可以使用聚合.以下是一个例子:

class TestClass
{
    public int I { get; set; }
    public string S { get; set; }
}

class Program
    {
        static void Main()
        {
            var myclass= new TestClass[]
            {
                new TestClass {I = 1, S = "1"}, new TestClass { I = 1, S = "11" }, new TestClass { I = 1, S = "111" },
                new TestClass {I = 2, S = "2"},new TestClass {I = 2, S = "222"},new TestClass {I = 2, S = "2"},
                new TestClass {I = 3, S = "3"},new TestClass {I = 3, S = "333"},new TestClass {I = 3, S = "33"},
                new TestClass {I = 4, S = "44"},new TestClass {I = 4, S = "4"},
                new TestClass {I = 5, S = "5"}
            };

            var filteredObject = myclass.GroupBy(x => x.I).Select(y => new
            {
                I = y.Key,
                S = y.Select(z => z.S).Aggregate((a, b) => (Convert.ToInt32(a) + Convert.ToInt32(b)).ToString())
            });

            filteredObject.ToList().ForEach(x => Console.WriteLine(x.I + "    " + x.S));
        }

    }

结果就像:

1 123

2 226

3 369

4 48

5 5

按任何键继续. . .

希望它有帮助.

其他推荐答案

组成的组.比您可以做的:

.GroupBy(x => x.Id)
.Select(x => new { x.Key, x.Sum(y => (int?)y.Quantity) });

本文地址:https://www.itbaoku.cn/post/1556993.html

问题描述

Here I have 2 Columns like Id,Quantity. I want get the Distinct Id and its Quantity Sum in LINQ. I tried like this

var r = list.Where(some operation)
            .Select(x => x.Id)
            .Distinct()
            .Select(x => (int?)x.Quantity))
            .Sum();

Here at x.Quantity I got error..how can I solve this.. please give your suggestions

推荐答案

If I assume that Id is int and Quantity is string, then you can use aggregate also. Below is one example:

class TestClass
{
    public int I { get; set; }
    public string S { get; set; }
}

class Program
    {
        static void Main()
        {
            var myclass= new TestClass[]
            {
                new TestClass {I = 1, S = "1"}, new TestClass { I = 1, S = "11" }, new TestClass { I = 1, S = "111" },
                new TestClass {I = 2, S = "2"},new TestClass {I = 2, S = "222"},new TestClass {I = 2, S = "2"},
                new TestClass {I = 3, S = "3"},new TestClass {I = 3, S = "333"},new TestClass {I = 3, S = "33"},
                new TestClass {I = 4, S = "44"},new TestClass {I = 4, S = "4"},
                new TestClass {I = 5, S = "5"}
            };

            var filteredObject = myclass.GroupBy(x => x.I).Select(y => new
            {
                I = y.Key,
                S = y.Select(z => z.S).Aggregate((a, b) => (Convert.ToInt32(a) + Convert.ToInt32(b)).ToString())
            });

            filteredObject.ToList().ForEach(x => Console.WriteLine(x.I + "    " + x.S));
        }

    }

Result is like:

1 123

2 226

3 369

4 48

5 5

Press any key to continue . . .

Hope it helps.

其他推荐答案

Group by Id. Than you can do :

.GroupBy(x => x.Id)
.Select(x => new { x.Key, x.Sum(y => (int?)y.Quantity) });