0

i have table looks like below

ID | Reason | PrID
----------------- 
1   abc      null
2   dhe      null
3   aerc      1
4   dwes      2
5   adfje     1

i have class

 public class Reason
{
    public int Id { get; set; }
    public string Reson{ get; set; }
    public List<SecondryReason> SecReason{ get; set; }
    public int? PrimaryId { get; set; }
}
public class SecondryReason
{
    public int Id { get; set; }
    public string Reason { get; set; }
    public int PrimaryReasonId { get; set; }
}

I want this to be displayed in hierarchy level if the prid is Null need to treat this as the parent remaining all child

i am trying Linq and unable to achieve this

Suggest me how to do this in an easy way in linq

Satti
  • 559
  • 2
  • 8
  • 26

2 Answers2

1

So: You have a list/enumerable of type , whereof the SecReason List property is null. Then, using linq you want a list, were the only the "root" reasons remain, but the Sub-reasons got put in the lists, but as type SecondaryReason? If so, I found this way to do it (linq and foreach):

    static IEnumerable<Reason> GetReasonsGrouped(List<Reason> reasons)
    {
        var result = reasons.Where(x => x.PrimaryId == null);
        foreach (var item in result)
        {
            item.SecReason = reasons.Where(x => x.PrimaryId == item.Id)
                                    .Select(x => new SecondryReason()
                                                      { Id = x.Id,
                                                        ReasonName = x.ReasonName,
                                                        PrimaryReasonId = item.Id
                                                       })
                                    .ToList();
        }

        return result;
    }

Or just linq, but harder to read:

  var result = reasons.Where(x => x.PrimaryId == null)
            .Select(x =>
            {
                x.SecReason = reasons.Where(r => x.PrimaryId == x.Id)
                                     .Select(r => new SecondryReason()
                                     {
                                         Id = r.Id,
                                         ReasonName = x.ReasonName,
                                         PrimaryReasonId = x.Id
                                     })
                                     .ToList();
                return x;
            });
Malior
  • 1,221
  • 8
  • 16
1

Not sure if linq will be the best solution, here is my proposed changes and method to get an Hierarchy type:

public class Reason
{
  public int Id { get; set; }
  public string Reson { get; set; }
  public List<Reason> SecReason { get; set; }
  public int? PrimaryId { get; set; }

  //Adds child to this reason object or any of its children/grandchildren/... identified by primaryId
  public bool addChild(int primaryId, Reason newChildNode)
  {
    if (Id.Equals(primaryId))
    {
      addChild(newChildNode);
      return true;
    }
    else
    {
      if (SecReason != null)
      {
        foreach (Reason child in SecReason)
        {
          if (child.addChild(primaryId, newChildNode))
            return true;
        }
      }
    }
    return false;
  }

  public void addChild(Reason child)
  {
    if (SecReason == null) SecReason = new List<Reason>();
    SecReason.Add(child);
  }

}

private List<Reason> GetReasonsHierarchy(List<Reason> reasons)
{
  List<Reason> reasonsHierarchy = new List<Reason>();

  foreach (Reason r in reasons)
  {
    bool parentFound = false;
    if (r.PrimaryId != null)
    {
      foreach (Reason parent in reasonsHierarchy)
      {
        parentFound = parent.addChild(r.PrimaryId.Value, r);
        if (parentFound) break;
      }
    }
    if (!parentFound) reasonsHierarchy.Add(r);
  }
  return reasonsHierarchy;
}
Darrel K.
  • 1,611
  • 18
  • 28