[英]order by List of Guids position
I have the following: 我有以下内容:
var nodes = _nodeService.GetNodeChildren(id, nId);
var association = _nodeService.GetNodeOrder(id, nId);
var joinedNodes = nodes.Join(association,
n => n.Id,
a => a,
(n, a) => new {nodes = n, association = a};
var enumerable = joinedNodes.ToList();
var orderedNodes = enumerable.OrderBy(x => x.association);
return orderedNodes.nodes;
where nodes is a list of objects and association is a list of GUIDs. 其中节点是对象列表,关联是GUID列表。
Issue is I'm not getting the order back as i expected. 问题是我没有按我的预期拿回订单。
I need to return a list of nodes based in the order that the GUIDS positions are. 我需要根据GUIDS位置的顺序返回节点列表。
the following is part of the test code 以下是测试代码的一部分
_ngOrder = new List<Guid>
{
_nodeId2,
_nodeId1,
_nodeId4,
_nodeId3,
}
_nodeClient.SetNodeOrder(_ngOrder);
When i get the method and assert here is the code: 当我得到方法并在这里断言是代码:
Assert.That(_response[0].Id, Is.EqualTo(_node2Id);
Assert.That(_response[1].Id, Is.EqualTo(_node1Id);
Assert.That(_response[2].Id, Is.EqualTo(_node4Id);
Assert.That(_response[3].Id, Is.EqualTo(_node3Id);
When using LINQ-to-objects and calling a join, then the outer list will be iterated and matching elements from the inner list will be taken accordingly to the selected key. 当使用LINQ-to-objects并调用联接时,外部列表将被迭代,并且内部列表中的匹配元素将与所选键相对应。 If you switch the lists in your join statement, you should achieve your goal.
如果在join语句中切换列表,则应实现目标。
var nodes = Enumerable.Range(1, 10).Select((id, index) => new Node { Id = Guid.NewGuid(), Name = "Name " + index }).ToList();
var ordering = nodes.OrderBy(node => node.Id).Select(node => node.Id);
// By making the ordering list the outer list, all elements will be sorted by this list.
var join = ordering.Join(nodes, o => o, n => n.Id, (o, n) => n);
Console.WriteLine("Unordered List");
foreach (var node in nodes)
{
Console.WriteLine($"{node.Name} => {node.Id}");
}
Console.WriteLine("Ordering");
foreach (var order in ordering)
{
Console.WriteLine(order);
}
Console.WriteLine("Reordered list");
foreach (var node in join)
{
Console.WriteLine($"{node.Name} => {node.Id}");
}
Console.ReadKey();
Output: 输出:
Unordered List
Name 0 => fb816820-4de2-4ece-b7db-1650c3ad84bc
Name 1 => 60fa1958-a54b-46ac-b6b9-957a92a56049
Name 2 => a3edf6da-6c3c-4836-99e8-ce6fa49e4b5c
Name 3 => 7b610a6d-7da6-4801-8437-2c73ed86ff9f
Name 4 => ce67987d-65f4-4020-b90b-27202f67c757
Name 5 => 62dd5df5-43f6-4c4a-ae66-6767d8bf232a
Name 6 => 10eae955-8675-450b-b10b-c973451b16b4
Name 7 => f6ccdac9-c34f-41a8-80f4-414da9cd1b0f
Name 8 => 49c57da8-a644-48a1-bc36-1bd3e10bd48e
Name 9 => 8d966e7c-ea90-4771-932c-5a8069c1a400
Ordering
10eae955-8675-450b-b10b-c973451b16b4
49c57da8-a644-48a1-bc36-1bd3e10bd48e
60fa1958-a54b-46ac-b6b9-957a92a56049
62dd5df5-43f6-4c4a-ae66-6767d8bf232a
7b610a6d-7da6-4801-8437-2c73ed86ff9f
8d966e7c-ea90-4771-932c-5a8069c1a400
a3edf6da-6c3c-4836-99e8-ce6fa49e4b5c
ce67987d-65f4-4020-b90b-27202f67c757
f6ccdac9-c34f-41a8-80f4-414da9cd1b0f
fb816820-4de2-4ece-b7db-1650c3ad84bc
Reordered list
Name 6 => 10eae955-8675-450b-b10b-c973451b16b4
Name 8 => 49c57da8-a644-48a1-bc36-1bd3e10bd48e
Name 1 => 60fa1958-a54b-46ac-b6b9-957a92a56049
Name 5 => 62dd5df5-43f6-4c4a-ae66-6767d8bf232a
Name 3 => 7b610a6d-7da6-4801-8437-2c73ed86ff9f
Name 9 => 8d966e7c-ea90-4771-932c-5a8069c1a400
Name 2 => a3edf6da-6c3c-4836-99e8-ce6fa49e4b5c
Name 4 => ce67987d-65f4-4020-b90b-27202f67c757
Name 7 => f6ccdac9-c34f-41a8-80f4-414da9cd1b0f
Name 0 => fb816820-4de2-4ece-b7db-1650c3ad84bc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.