Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Wednesday, 16 January 2019

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

 7 comments   

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 { get; set; }
  public List<SubList> SubLists { get; set; }
}

public class SubList
{
  public int SubListID { get; set; }
  public string SubListName { get; set; }
}



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();
        }
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

7 comments:

  1. janani ram25 December 2019 at 21:13

    Thanks for the informative article. This is one of the best resources I have found in quite some time.
    Xamarin Training in Chennai
    Xamarin Course in Chennai
    German Classes in Chennai
    IELTS Coaching in Chennai
    Japanese Classes in Chennai
    Spoken English Classes in Chennai
    spanish classes in chennai
    content writing course in chennai
    Xamarin Training in OMR
    Xamarin Training in Porur

    ReplyDelete
    Replies
    1. for ict 9918 September 2020 at 12:10

      This comment has been removed by a blog administrator.

      Delete
      Replies
        Reply
    2. Reply
  2. telkahost10 April 2020 at 02:38

    This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
      Reply
  3. customer paper15 May 2020 at 10:58

    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
    Replies
      Reply
  4. Pathway for German Language22 December 2020 at 00:29

    This comment has been removed by the author.

    ReplyDelete
    Replies
      Reply
  5. vé máy bay từ canada về Việt Nam22 February 2021 at 00:11

    This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
      Reply
  6. Links For You15 September 2022 at 09:55

    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
    Replies
      Reply
Add comment
Load more...

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write