0

I am looking forward to get a linq query for populating list of teachers and their respective divisons. Here I have 2 classes Teacher and Division which are related by DivisionGroupID - GroupID

public class Teacher
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Division> lstDivison {get;set;}
    public int DivisionGroupID { get; set; }
    
}

public class Division
{
    public int GroupID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }  
}

In main method List of both Teacher and Division will be populated

      static void Main(string[] args)
    {
        Teacher obj = new Teacher { ID = 1, DivisionGroupID = 11, Name = "abcd" };
        Teacher obj1 = new Teacher { ID = 2, DivisionGroupID = 12, Name = "efgh" };
        List<Teacher> objList = new List<Teacher>();
        objList.Add(obj);
        objList.Add(obj1);
        Division dv = new Division { GroupID = 11 ,Name="Division1",Description="first" };
        Division dv1 = new Division { GroupID = 11, Name = "Division2", Description = "second" };
        Division dv2 = new Division { GroupID = 11, Name = "Division3", Description = "third" };
        Division dv3 = new Division { GroupID = 12, Name = "Division4", Description = "fourth" };
        Division dv4 = new Division { GroupID = 12, Name = "Division5", Description = "fifth" };
        Division dv5 = new Division { GroupID = 12, Name = "Division6", Description = "sixth" };
        List<Division> lstDiv = new List<Division>();
        lstDiv.Add(dv);
        lstDiv.Add(dv1);
        lstDiv.Add(dv2);
        lstDiv.Add(dv3);
        lstDiv.Add(dv4);
        lstDiv.Add(dv5);

}

The requirement here is to get the list of teachers and populate the sublist of divisions each teachers holding. I got the solution based on 2 approaches.

  1. Using sub query approach :

var upd = from teacher in objList
          select new Teacher
          {
              ID = teacher.ID,
              Name = teacher.Name,
              lstDivison = (from div in lstDiv
                            where div.GroupID == teacher.DivisionGroupID
                            select new Division
                            {
                                Name = div.Name,
                                Description = div.Description
                            }).ToList()
          };
  1. Using Foeach loop through Teacher collection(objList) and updating the lstDivision

  objList.ForEach(x => x.lstDivison = lstDiv
    .Where(y => y.GroupID == x.DivisionGroupID)
    .Select(p => new Division { Name = p.Name, Description = p.Description })
    .ToList());

Both of these approaches will give me the result. But i am looking forward a better approach in as part of my project requirement which has to improve the query performance. Could you please suggest which is the best approach to handle this situation?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Renz
  • 1
  • _"Using Foeach loop"_ Fyi, you are using [`List.ForEach`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=net-5.0) which is not a [`foreach`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement)-loop. It's also using a loop internally but it's a method. – Tim Schmelter May 31 '21 at 14:35

1 Answers1

0

use yours teacher object to populate list of divisions under it. as my understanding that how it was designed class structure.

   //populate property in object
    objList.ForEach(x => {
        x.lstDivison = lstDiv.Where(w=> w.GroupID == x.DivisionGroupID).ToList();   
    });

    objList.Dump();

enter image description here

Power Mouse
  • 727
  • 6
  • 16