Home | Table Design |
Any - Simple
This sample uses Any to determine if any of the words in the array contain the substring 'iaa'.
DataClassesDataContext db = new DataClassesDataContext();
LINQ/ Lambda:
var result = (from c in db.Customers
select c).Any(c => c.Name.Contains("iaa"));
Response.Output.WriteLine("There is a word that contain iaa : {0}<br> ", result);
Result:
There is a word that contain iaa : False
Any - Grouped
This sample uses Any to return a grouped a list of Customer only for Education that have at least one “2” education.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from c in db.Customers
group c by c.Sex into CustGroup
where CustGroup.Any(p => p.Education == "2")
select new { CustGroup.Key, CustGroup };
foreach (var item in result)
{
Response.Output.WriteLine("Key : {0}<br> ", item.Key);
foreach (var value in item.CustGroup)
Response.Output.WriteLine("Name : {0}<br>", value.Name);
}
Result:
Key : 1
Name : Rachit Rokad
Name : Ayush Patel
All - Simple
This sample uses All to determine whether records contains only MCA.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from value in db.Customers
select value).All(n => n.Education == "MCA");
Lambda:
var result = db.Customers.All(n => n.Education == "MCA");
Response.Output.WriteLine("The list contains MCA : {0}<br>", result);
Result:
The list contains MCA : False
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from value in db.Customers
where value.Age > 24
select value).All(n => n.Education == "MCA");
Lambda:
var result = db.Customers.Where(n => n.Age>24).All(n => n.Education == "MCA");
Response.Output.WriteLine("The list contains MCA : {0}<br>", result);
Result:
The list contains MCA : True
All - Grouped
This sample uses All to return a grouped a list of Customer only for Education that have all of them are in MCA and age higher than 25.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from value in db.Customers
where value.Age>25
group value by value.Sex into Cust
where Cust.All(p => p.Education == "MCA")
select new { Cust.Key, Cust };
foreach (var item in result)
{
Response.Output.WriteLine("Sex: {0}<br>", item.Key);
foreach (var ite in item.Cust)
Response.Output.WriteLine("Name: {0}<br>", ite.Name);
}
Result
Sex: 1
Name: Rachit Rokad
==============================
Shradhdha Zalavadiya |Senior Software Engineer
WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in
No comments:
Post a Comment