0

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);
Liam
  • 27,717
  • 28
  • 128
  • 190
lemunk
  • 2,616
  • 12
  • 57
  • 87
  • What are the types of nodes and association? – Ben Jan 24 '19 at 14:16
  • Why are you using GroupJoin? Does single node can have many associations? – Jonatan Dragon Jan 24 '19 at 14:17
  • sorry edited should be join not grp join, and ben the types are list of objects and list og guids as mentioned – lemunk Jan 24 '19 at 14:22
  • then now do: association.Join(nodes, not nodes.Join(association – Jonatan Dragon Jan 24 '19 at 14:26
  • 1
    Note that a GUID is actually a numeric value which is often represented in a formatted way (Hex values and some dashes). Maybe the numeric sorting can differ from the alphabetical sorting of the representation? – GolezTrol Jan 24 '19 at 14:36
  • the sorting i need is however the Guids are stored in there position in the table. i then need to use that to order the nodes associated with the GUIDs and return them – lemunk Jan 24 '19 at 14:39
  • We need examples of the GUIDs and what order you expect them to be in. It's not entirely obvious how a GUID should be ordered. do you want the hex value order, or based on the string value? – Liam Jan 24 '19 at 14:39
  • If the GUID is not a primary key it does not have a position in a table – Liam Jan 24 '19 at 14:40
  • Association is a GUID or a LIST OF GUIDS? If it's a list, your sorting doesn't make much sense as it is right now... – Josef Ginerman Jan 24 '19 at 14:41
  • BTW using GUIDs as the primary key on a DB table has a [lot of drawbacks](https://stackoverflow.com/a/11938495/542251) and should be avoided wherever possible. The only real advantage is being able to be unique across systems/other tables, if you don't need this, use an integer instead. – Liam Jan 24 '19 at 14:44
  • Your edit hasn't helped at all – Liam Jan 24 '19 at 14:45
  • Its a Cassandra table, it has other values but we only return the Guids that are the node Guids. – lemunk Jan 24 '19 at 14:45

1 Answers1

5

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
Oliver
  • 43,366
  • 8
  • 94
  • 151