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();
}
|
0 comments:
Post a Comment