This Blog Use For Major operation of List<> in C#
Introduction
Lists are considered generics and constructed types. You need to use < and > in the List declaration.
List <> is Following Step
1) Create Class for List
Hide
Expand
});
Hide
Expand
Hide
Expand
Hide
Expand
Hide
Expand
Hide
Expand
This Very useful in WPF
Introduction
Lists are considered generics and constructed types. You need to use < and > in the List declaration.
List <> is Following Step
1) Create Class for List
public class AddROSublet
{
string _Id;
string _FaultId;
string _VendorName;
string _VendorId;
public string
VendorName
{
get
{
return _VendorName;
}
set
{
_VendorName = value;
}
}
public string
VendorId
{
get
{
return _VendorId;
}
set
{
_VendorId = value;
}
}
}
|
2) Create object For List and Add Value in List
public List<Test> lParts = new
List<Test>();
lParts.Add(new Test()
{
Id = 1,
FId = “9999”,
Name =”Tejas”,
vId = 1003
});
lParts.Add(new Test()
{
Id = “2”,
FId = “9999”,
Name =”Dhaval”,
vId = 1002
});
lParts.Add(new Test()
{
Id = “3”,
FId = “9999”,
Name =”Amit”,
vId = 1001
|
3)List Operation Related SpecificId one
column,ordr by,Gorup by
///This Code Give All Record for Spcific ID
var names1 = lParts.Where(n => n. vId == "1003").ToList();
///This Code Give only one value of Column
var names1 = lParts.Where(n => n. vId == "1003")
.Select(n
=> n.VendorId).ToList();
/// This Code Give only one value of Column in one range
ex(1,0,0,3)
var names2 = lParts.Where(n => n. vId == "1003")
.SelectMany(n
=> n.VendorId).ToList();
///This Code For Group by and order
by
var groupedList = lParts.GroupBy(c => new
{ c.Name, c.VendorId })
.OrderBy(g
=> g.Key.Name).ThenBy(g => g.Key.VendorId).ToList();
///This For Group by
var groupedList1 = lParts.GroupBy(c => new
{ c.Name, c.VendorId })
.ToList();
//This Code for order by
var groupedList2 = lParts.OrderBy(g => g.Name).ToList();
//This code for Sum
var sums = lParts.Sum(x => Convert.ToInt64(x.Id));
|
4) List Operation Related Find Index
//Method 1 for find index
int index = lParts.FindIndex(lparts => lparts.Id.Equals(Convert.ToString(“12345”),
StringComparison.Ordinal));
//Method 2 for find index
int index = lParts.FindIndex(
delegate(Test sublet)
{
return
sublet.Id == Convert.ToString(“12345”);
});
int indexChild = lPartsChild.FindIndex(
delegate(Test sublet)
{
return
sublet.Id == Convert.ToString(“12345”);
});
//Method 3 for find index
int index = lParts.FindIndex(
delegate(Test sublet)
{
return
sublet.Id.Equals(“12345”, StringComparison.Ordinal);
});
//This Methos Find and then give last index
int firstIndex = lTest.FindIndex(FindTest);
//This Methos Find and then give last index
int lastIndex = lTest.FindLastIndex(FindTest);
//region Index of first in the second half of the collection
int mid = lTest.Count / 2;
int halfIndex = lTest.FindIndex(mid, mid, FindTest);
#endregion
//region Index of last in the second half of the collection
int halfLastIndex = lTest.FindLastIndex(lTest.Count - 1,
mid, FindTest);
// Explicit predicate delegate.
private static bool FindTest(Test tst)
{
if (tst.MainId ==
Textbox1.Text)
return true;
else
return false;
}
|
5) List Operation Related to Find Operation
//Search and Update value by index value
int indexVA = -1;
indexVA = lParts.FindIndex(lparts
=> Convert.ToString(lparts.FName).ToLower() == Convert.ToString(“tejas”);
if (indexVA >= 0)
{
//lTest[indexVA].Id = 1;
//lTest[indexVA].FName = txtSearch.Text;
lParts
[indexVA]. FId =
100;
this.viewMain.ItemsSource = null;
this.viewMain.ItemsSource = lTest;
this.viewMain.SelectedIndex = indexVA;
}
//Find First Record only
Test objTest = lTest.Find(
delegate(Test objT)
{
return objT.FId == Textbox1.Text;
}
);
///This For find only first record
from list
var item = lParts.FirstOrDefault(x => x. vId == "1003");
///This for find only single record
from list
var item1 = lParts.SingleOrDefault(x => x.FId == "9999");
//Find Last Record only
Test objLastTest = lTest.FindLast(
delegate(Test objT)
{
return objT. FId ==
Textbox1.Text;
}
);
//Find All Record only
lTest = lTest.FindAll(
delegate(Test objT)
{
return objT. FId ==
Textbox1.Text;
}
);
//This code for give range of array
Test[] output = lTest.GetRange(2, 3).ToArray();
|
6)List Operation Related Remove Operation
#region Remove from list
lTest.Remove(lTest.Find(delegate(Test
objT)
{
return Convert.ToString(objT.Name).ToLower()
== “tejas”;
}));
#region Remove All from list
if (Convert.ToString(txtRemove.Text)
!= "")
{
lTest.RemoveAll(delegate(Test objT)
{
return Convert.ToString(objT.FName).ToLower()
== Convert.ToString(“tejas”).ToLower();
});
}
#region Remove from index
int indx = lTest.FindIndex(ltest => Convert.ToString(ltest.FName).ToLower() ==
“tejas”);
if (indx >= 0)
lTest.RemoveAt(indx);
#endregion
// Remove from range of class
if (indx >= 0)
lTest.RemoveRange(indx, 3);
// This Code For Reverse List
lTest.Reverse();
|
This Very useful in WPF
nice work