下面是在我试图得到一个用户只需顶部最新版本记录所有客户端和角色代码。因此,在下面is..this输出工作在下面,但独立模式很好,当我在我的项目是基于.NET核心的2.1.x添加以下代码提示错误“无法比较数组中的两个元素。\ r \ n至少有一个对象必须实现IComparable”,如何解决它,任何建议?
Solution #1
4 VIEW 1
3 ADMIN 2
码
using System;
using System.Linq;
public class Simple {
public static void Main() {
var userRoles = (new[]
{
new { clientid=1 , rowVersion = 1 , role="READ" },
new { clientid=1 , rowVersion = 2 , role="EDIT" },
new { clientid=2 , rowVersion = 3 , role="ADMIN" },
new { clientid=1 , rowVersion = 4 , role="VIEW" }
});
var results = userRoles.GroupBy(x => x.clientid)
.Select(x => x.OrderByDescending(y => y.rowVersion).First());
Console.WriteLine("Solution #1");
foreach (var k in results)
{
Console.WriteLine("{0} {1}", k.rowVersion, k.role, k.clientid);
}
}
}
UPDATE得到了解决下面添加作为回答。
根本原因是“匿名类型没有可比性订购。”
下面是上述问题的有效的解决方案。问题是“匿名类型没有可比性订购。”
var results = userRoles
.OrderByDescending(y => y.rowVersion)
.GroupBy(x => x.clientid)
.SelectMany(x => x.Take(1));
这工作都好。