Skip to main content
added 8 characters in body
Source Link
user73941
user73941
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();
  List<Node<T>> roots = null;

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var rootparent = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (rootparent != null)
    {
      rootparent.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = root;parent;
      }
    }
    else
    {
      roots = group.ToList();
    }
  }

  return roots; //nodes.Where(n => IsRootNode(n.Value)).ToList();
}
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();
  List<Node<T>> roots = null;

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var root = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (root != null)
    {
      root.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = root;
      }
    }
    else
    {
      roots = group.ToList();
    }
  }

  return roots; //nodes.Where(n => IsRootNode(n.Value)).ToList();
}
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();
  List<Node<T>> roots = null;

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var parent = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (parent != null)
    {
      parent.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = parent;
      }
    }
    else
    {
      roots = group.ToList();
    }
  }

  return roots; //nodes.Where(n => IsRootNode(n.Value)).ToList();
}
added 115 characters in body
Source Link
user73941
user73941
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();
  List<Node<T>> roots = null;

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var root = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (root != null)
    {
      root.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = root;
      }
    }
    else
    {
      roots = group.ToList();
    }
  }

  return roots; //nodes.Where(n => IsRootNode(n.Value)).ToList();
}
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var root = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (root != null)
    {
      root.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = root;
      }
    }
  }

  return nodes.Where(n => IsRootNode(n.Value)).ToList();
}
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var nodes = flatDataObject.Select(o => new Node<T> { Value = o }).ToArray();
  List<Node<T>> roots = null;

  foreach (var group in nodes.GroupBy(n => n.Value.ParentId))
  {
    var root = nodes.FirstOrDefault(n => n.Value.Id == group.Key);
    if (root != null)
    {
      root.Children.AddRange(group);
      foreach (var node in group)
      {
        node.Parent = root;
      }
    }
    else
    {
      roots = group.ToList();
    }
  }

  return roots; //nodes.Where(n => IsRootNode(n.Value)).ToList();
}
deleted 412 characters in body
Source Link
user73941
user73941
public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var lookup = new Dictionary<int, Node<T>>();
  var rootNodes = new List<Node<T>>();

  foreach (T element in flatDataObject)
  {
    if (!lookup.TryGetValue(element.Id, out Node<T> currentNode))
    {
      currentNode.Value = element;
    }
    else
    {
      currentNode = new Node<T>() { Value = element };
      lookup.Add(element.Id, currentNode);
    }

    if (IsRootNode(element))
    {
      rootNodes.Add(currentNode);
    }
    else
    {          
      if (!lookup.TryGetValue(element.ParentId, out Node<T> parentNode))
      {
        parentNode = new Node<T>();
        lookup.Add(element.ParentId, parentNode);
      }
      parentNode.Children.Add(currentNode);
      currentNode.Parent = parentNode;
    }
  }

  return rootNodes;
}

Here the for-loop is changed to a foreach-loop which makes it a little more readable.

The first lookup.TryGetValue():

if (lookup.TryGetValue(flatDataObject[element].Id, out currentNode))
{
    currentNode.NodeInformation = flatDataObject[element];
}
else
{
    currentNode = new GenericNode<T>() { NodeInformation = flatDataObject[element] };
    lookup.Add(flatDataObject[element].Id, currentNode);
}

is negated because if the node is found in lookup there's no need to set NodeInformation again.

public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var lookup = new Dictionary<int, Node<T>>();
  var rootNodes = new List<Node<T>>();

  foreach (T element in flatDataObject)
  {
    if (!lookup.TryGetValue(element.Id, out Node<T> currentNode))
    {
      currentNode = new Node<T>() { Value = element };
      lookup.Add(element.Id, currentNode);
    }

    if (IsRootNode(element))
    {
      rootNodes.Add(currentNode);
    }
    else
    {          
      if (!lookup.TryGetValue(element.ParentId, out Node<T> parentNode))
      {
        parentNode = new Node<T>();
        lookup.Add(element.ParentId, parentNode);
      }
      parentNode.Children.Add(currentNode);
      currentNode.Parent = parentNode;
    }
  }

  return rootNodes;
}

Here the for-loop is changed to a foreach-loop which makes it a little more readable.

The first lookup.TryGetValue():

if (lookup.TryGetValue(flatDataObject[element].Id, out currentNode))
{
    currentNode.NodeInformation = flatDataObject[element];
}
else
{
    currentNode = new GenericNode<T>() { NodeInformation = flatDataObject[element] };
    lookup.Add(flatDataObject[element].Id, currentNode);
}

is negated because if the node is found in lookup there's no need to set NodeInformation again.

public static List<Node<T>> CreateTree<T>(List<T> flatDataObject, Func<T, bool> IsRootNode) where T : IDbEntityNode
{
  var lookup = new Dictionary<int, Node<T>>();
  var rootNodes = new List<Node<T>>();

  foreach (T element in flatDataObject)
  {
    if (lookup.TryGetValue(element.Id, out Node<T> currentNode))
    {
      currentNode.Value = element;
    }
    else
    {
      currentNode = new Node<T>() { Value = element };
      lookup.Add(element.Id, currentNode);
    }

    if (IsRootNode(element))
    {
      rootNodes.Add(currentNode);
    }
    else
    {          
      if (!lookup.TryGetValue(element.ParentId, out Node<T> parentNode))
      {
        parentNode = new Node<T>();
        lookup.Add(element.ParentId, parentNode);
      }
      parentNode.Children.Add(currentNode);
      currentNode.Parent = parentNode;
    }
  }

  return rootNodes;
}

Here the for-loop is changed to a foreach-loop which makes it a little more readable.

added 16 characters in body
Source Link
user73941
user73941
Loading
added 678 characters in body
Source Link
user73941
user73941
Loading
added 2438 characters in body
Source Link
user73941
user73941
Loading
Source Link
user73941
user73941
Loading