如何在Linq中的单个查询中使用Distinct()和Sum()
这里我有两个专栏,比如 身份证,数量。我想得到不同的Id和它在LINQ中的数量和。 我试过这样做如何在Linq中的单个查询中使用Distinct()和Sum(),linq,c#-4.0,Linq,C# 4.0,这里我有两个专栏,比如 身份证,数量。我想得到不同的Id和它在LINQ中的数量和。 我试过这样做 var r = list.Where(some operation) .Select(x => x.Id) .Distinct() .Select(x => (int?)x.Quantity)) .Sum(); 这里是x.数量我有错误..我怎么解决这个。。 请按Id分组给出您的建议。您可以:
var r = list.Where(some operation)
.Select(x => x.Id)
.Distinct()
.Select(x => (int?)x.Quantity))
.Sum();
这里是x.数量我有错误..我怎么解决这个。。
请按Id分组给出您的建议。您可以:
.GroupBy(x => x.Id)
.Select(x => new { x.Key, x.Sum(y => (int?)y.Quantity) });
按Id分组。您可以执行以下操作:
.GroupBy(x => x.Id)
.Select(x => new { x.Key, x.Sum(y => (int?)y.Quantity) });
如果我假设Id是int,数量是string,那么您也可以使用aggregate。以下是一个例子:
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));
}
}
结果如下:
1123
2226
3369
448
5.5
按任意键继续
希望有帮助。如果我假设Id是int,Quantity是string,那么您也可以使用aggregate。以下是一个例子:
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));
}
}
结果如下:
1123
2226
3369
448
5.5
按任意键继续
希望有帮助。错误消息是什么?错误40'int'不包含'Quantity'的定义,并且找不到接受第一个'int'类型参数的扩展方法'Quantity'(是否缺少using指令或程序集引用?错误消息是什么?错误40'int'不包含'Quantity'的定义,并且找不到接受'int'类型的第一个参数的扩展方法'Quantity'(您是否缺少using指令或程序集引用?我已重新格式化并进行了更正。
GroupBy的结果中没有Value
,仍然存在错误。)Displayed@benz您只需保留var r=list.Where(某些操作)
部分代码,删除所有其他内容,然后添加此响应中显示的代码。class.Quantity=列表。其中(x=>x.StatusId==(int)Enum.Attached&&x.StatusId!=3)。GroupBy(x=>x.JobId)。选择(x=>new{x.Key,x.Sum(y=>(int?)y.Quantity)});为什么要将数量强制转换为可为空的整数?我已重新格式化并进行了更正。GroupBy的结果中没有Value
,仍然存在错误Displayed@benz您只需保留var r=list.Where(某些操作)
部分代码,删除所有其他内容,然后添加此响应中显示的代码。class.Quantity=列表。其中(x=>x.StatusId==(int)Enum.Attached&&x.StatusId!=3)。GroupBy(x=>x.JobId)。选择(x=>new{x.Key,x.Sum(y=>(int?)y.Quantity)});为什么要将数量强制转换为可为空int?