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

 No 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
Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Popular Posts

  • Trigger In SQL Server - Insert Update Delete In Single Trigger With Example
    Introduction In this article we will learn what is trigger . How to manage Insert, Update and Delete operation in trigger. Insert Update ...
  • Base64 Encoding And Decoding In SQL Server
    Introduction In this article we will learn How to encode a string into Base64String and how to decode Base64String to string. How to crea...
  • Convert DataSet To List Or GenericList Using LINQ - C#
    Introduction In this article we will learn how to convert dataset to list or convert DataSet to List of collection o generic list in c#. ...
  • Difference Between Rank(), Dense_Rank() And Row_Number() In SQL
    Introduction In this post we will learn what is the difference between Rank(), Dense_Rank() And Row_Number() and when do we need this. ...
  • What Is Connection Pooling In Asp.Net Using C# With Example
    Introduction In this article we will learn what is connection pooling. Why connection pooling useful , what is the max limit and minimum ...

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

Best Deal Here

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