Wednesday, 16 January 2019

Read List With In List Collection Using LINQ - Increase Application Data Performances

Introduction
In this article we will learn about how to read data from a SubList or list with in list collection. How to access sub list properties and how to get sublist property value using a single Linq line. 



How to perform ForEach  In Linq and perform faster execution.

Sometime while doing development, we faced some issue when retrieving and reading  data from List collection. When we have multiple list or list with in lists than it is a heptic situation to write lots of foreach loop in c# to read list item.

Foreach of c# is now tradional approach to iterate through loop but after linq comes in light , most of us prefer to write linq  simple and more effective single line querie to read data by using lambda expression or query expression.

Demo List

public class MainList
{
  public string MainListName { getset; }
  public List<SubList> SubLists { getset; }
}

public class SubList
{
  public int SubListID { getset; }
  public string SubListName { getset; }
}



Write Data Into List Using Console Application

 class Program
 {
   static List<MainList> MainListData = new List<MainList>();

   static void Main(string[] args)
   {
       FillDataInList();
       Console.ReadLine();
   }

   static void FillDataInList()
   {
       for (int i = 1; i < 5; i++)
       {
           MainList tempList = new MainList();
           tempList.MainListName = "MainList Item " + i;

           tempList.SubLists = new List<SubList>();

           SubList objSublist = new SubList();
           objSublist.SubListID = i;
           objSublist.SubListName = "SubList Item" + i;
           tempList.SubLists.Add(objSublist);
           MainListData.Add(tempList);
       }
   }
}

Read data using Traditional approach and using Linq

 foreach (var item in MainListData)
 {
     foreach (var subListItem in item.SubLists)
     {
         Console.WriteLine(subListItem.SubListName);
     }
     
 }

 //Using LINQ
 var SubListNames = MainListData.Select(x => x.SubLists)
                    .SelectMany(x => x)
                    .Select(x => x.SubListName).ToList();

Nested List Example

foreach (var company in Company)
{
   foreach (var employee in company.Employees)
   {
    foreach (var department in employee.Departments)
    {
     foreach (var team in department.Team)
     {
        //Read List here 
        if ( team.Technology == "Xamarin")
        {
           //do some code here 
        }
     }

   }
 }
}

 //Using LINQ 

 var listofList = Company.SelectMany(c => c.Employees
                .SelectMany(t => t.Departments
                .SelectMany(r => r.Team)))
                .Where(jp => jp.Technology = "Xamarin")
                .ToList();
        }

7 comments:

  1. Replies
    1. This comment has been removed by a blog administrator.

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. I faced such a problem lately and didn't really know what to do with this, very thankful to you for your detailed explanations.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Imagenomic Portraiture Crack enables us to accomplish excellence in portrait retouching by removing the arduous labour of choosing masking and pixel-by-pixel treatments. Portraiture Plugin For Photoshop CS6 Crack

    ReplyDelete