按向导列表位置排序

order by List of Guids position

我有以下内容:

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;

其中节点是对象列表,关联是 GUID 列表。

问题是我没有按预期取回订单。

我需要 return 一个基于 GUIDS 位置顺序的节点列表。

以下为部分测试代码

_ngOrder = new List<Guid>
{
    _nodeId2,
    _nodeId1,
    _nodeId4,
    _nodeId3,
}
_nodeClient.SetNodeOrder(_ngOrder);

当我得到方法并断言这里是代码时:

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);

当使用 LINQ-to-objects 并调用连接时,外部列表将被迭代,内部列表中的匹配元素将相应地与所选键匹配。如果您在连接语句中切换列表,您应该可以实现您的目标。

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();

输出:

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