C# n消息:无法比较数组中的两个元素“ #1- User,Role, Client Id, TimeStamp, rowVersion 201,READ,101, 1/18/2019, 2 201,WRITE,101, 1/18/2019, 1
n消息:无法比较数组中的两个元素“C# n消息:无法比较数组中的两个元素“ #1- User,Role, Client Id, TimeStamp, rowVersion 201,READ,101, 1/18/2019, 2 201,WRITE,101, 1/18/2019, 1,c#,.net,linq,dictionary,C#,.net,Linq,Dictionary,n消息:无法比较数组中的两个元素“ #1- User,Role, Client Id, TimeStamp, rowVersion 201,READ,101, 1/18/2019, 2 201,WRITE,101, 1/18/2019, 1 201,ADMIN,102, 1/18/2019, 3 202,READ,102, 1/19/2019, 12 201,READ,101, 1/18/2019, 2 201,ADMIN,102, 1/18/2019, 3
#1- User,Role, Client Id, TimeStamp, rowVersion
201,READ,101, 1/18/2019, 2
201,WRITE,101, 1/18/2019, 1
201,ADMIN,102, 1/18/2019, 3
202,READ,102, 1/19/2019, 12
201,READ,101, 1/18/2019, 2
201,ADMIN,102, 1/18/2019, 3
201,READ,101, 1/18/2019, 2
201,ADMIN,102, 1/18/2019, 3
select * from(
select UserId,ClientId,RowVersion,RoleCode,row_number()
over(partition by ClientId order by RowVersion desc) as roworder
from dbo.UR where UserId=6
) temp
where roworder=1
class ClientRole
{
public int ClientId { get; set; }
public string Role { get; set; }
public int RowVersion { get; set; }
}
var clientRoles = new List<ClientRole>
{
new ClientRole { ClientId = 101, Role = "READ", RowVersion = 2 },
new ClientRole { ClientId = 101, Role = "WRITE", RowVersion = 1 },
new ClientRole { ClientId = 102, Role = "ADMIN", RowVersion = 2 },
new ClientRole { ClientId = 102, Role = "OTHER", RowVersion = 12 }
};
var clientRolesDic = (
from cr in clientRoles
group cr by cr.ClientId into g
select g.OrderByDescending(i => i.RowVersion).First()
)
.ToDictionary(k => k.ClientId);
var results = list.GroupBy(x => x.ClientId)
.Select(x => x.OrderBy(y => y.rowVersion).Last())
.ToDictionary(x => x.ClientId);
var clientRoles = new []
{
new { ClientId = 101, Role = "READ", RowVersion = 2 },
new { ClientId = 101, Role = "WRITE", RowVersion = 1 },
new { ClientId = 102, Role = "ADMIN", RowVersion = 3 },
new { ClientId = 102, Role = "READ", RowVersion = 2 },
};
var result =
from cr in clientRoles
orderby cr.ClientId, cr.RowVersion descending
group cr by cr.ClientId into gs
from g in gs.Take(1)
select g;
new { ClientId = 101, Role = "READ", RowVersion = 2 },
new { ClientId = 102, Role = "ADMIN", RowVersion = 3 },
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 o = userRoles.OrderByDescending(x => x.rowVersion)
.GroupBy(x => x.clientid)
.SelectMany(g =>
g.Select((j, i) => new { j.rowVersion, j.role, rn = i + 1 })
);
var z = o.Select(x => new {x.rowVersion, x.role, x.rn}).Where(a => a.rn==1);
var results = userRoles.GroupBy(x => x.clientid)
.Select(x => x.OrderByDescending(y => y.rowVersion).First());
Console.WriteLine("Solution #1");
foreach (var i in z)
{
Console.WriteLine("{0} {1} {2}", i.rowVersion, i.role, i.rn);
}
Console.WriteLine("Solution #2");
foreach (var k in results)
{
Console.WriteLine("{0} {1}", k.rowVersion, k.role);
}
}
}