Home | Table Design |
ToArray
This sample uses ToArray to immediately evaluate a sequence into an array.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from c in db.Customers
select c.Name;
var NameArray = result.ToArray();
Lambda:
var result = db.Customers.Select(c => c.Name).ToArray();
for (int i = 0; i < NameArray.Length; i++)
Response.Output.WriteLine("{0}<br> ", NameArray[i]);
Result:
Rachit Rokad
Poonam Makati
Ayush Patel
Nenshi
Yuti Vachani
ToList
This sample uses ToList to immediately evaluate a sequence into a List<T>.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from c in db.Customers
select c.Name;
var NameList = result.ToList();
Lambda:
var result = db.Customers.Select(c => c.Name).ToList();
foreach (var item in NameList)
Response.Output.WriteLine("{0}<br> ", item);
Result:
Rachit Rokad
Poonam Makati
Ayush Patel
Nenshi
Yuti Vachani
ToDictionary
This sample uses ToDictionary to immediately evaluate a sequence and a related key expression into a dictionary.
DataClassesDataContext db = new DataClassesDataContext();
var result = from c in db.Customers
select new { c.CustID , c.Name, c.Education};
var NameDict = result.ToDictionary(sr => sr.CustID);
foreach (var item in NameDict)
Response.Output.WriteLine("{0} {1} {2}<br> ",
item.Key, item.Value.Name, item.Value.Education);
Result:
1 Rachit Rokad MCA
2 Poonam Makati MCA
3 Ayush Patel 2
4 Nenshi BE-Comm
5 Yuti Vachani 8
==============================
Shradhdha Zalavadiya |Senior Software Engineer
WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in
No comments:
Post a Comment