C# 使用linq按多个属性分组并返回匿名类型

C# 使用linq按多个属性分组并返回匿名类型,c#,sql,linq,C#,Sql,Linq,如何在linq中编写以下查询并将结果作为匿名对象返回 SELECT ProductId, ProductName FROM Items GROUP BY ProductId, ProductName 你会得到同样的结果 SELECT distinct ProductId, ProductName FROM Items 因此,选择这两列并对结果使用Distinct。您不需要在此处分组。使用不同的 Items.Select(i => new { i.ProductId, i.Product

如何在linq中编写以下查询并将结果作为匿名对象返回

SELECT ProductId, ProductName
FROM Items
GROUP BY ProductId, ProductName

你会得到同样的结果

SELECT distinct ProductId, ProductName
FROM Items

因此,选择这两列并对结果使用Distinct。

您不需要在此处分组。使用不同的

Items.Select(i => new { i.ProductId, i.ProductName }).Distinct();

和以往一样,你应该展示你已经尝试过的东西,以及你进行过的研究。非常感谢你给出的简洁明了的答案。工作得很好。