简体   繁体   中英

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.

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.

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. If you switch the lists in your join statement, you should achieve your goal.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM