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.
Write Data Into List Using Console Application
Read data using Traditional approach and using Linq
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; }
}
|
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);
}
}
}
|
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();
}
|
Thanks for the informative article. This is one of the best resources I have found in quite some time.
ReplyDeleteXamarin 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
This comment has been removed by a blog administrator.
DeleteThis comment has been removed by a blog administrator.
ReplyDeleteI faced such a problem lately and didn't really know what to do with this, very thankful to you for your detailed explanations.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteImagenomic 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