Saturday, September 4, 2010

Restriction Operators


Home Table Design

Where - Simple 1


This sample uses where to find all Customers having Age less than 18.

SQL:

SELECT CustID, Name, Sex, Education, IsStudent, Age, ContactNo
FROM Customer
WHERE Age < 18

DataClassesDataContext db = new DataClassesDataContext();

LINQ:

var result =
from c in db.Customers
where c.Age < 18
select c;

Lambda:

var result = db.Customers.Where(c => c.Age < 18);

Response.Output.WriteLine("Age < 18:<br>");

foreach (var cust in result)
Response.Output.WriteLine(cust.Name + " " + cust.Age + "<br>" );

Result:

Age < 18:  
Ayush Patel 7
Yuti Vachhani 12

 

Where - Simple 2


 This sample uses where to find all Customers that are Student.

SQL:

SELECT CustID, Name, Sex, Education, IsStudent, Age, ContactNo
FROM Customer
WHERE IsStudent = 1

DataClassesDataContext db = new DataClassesDataContext();

LINQ:

var result =
from c in db.Customers
where c.IsStudent == 1
select c;

Lambda:

var result = db.Customers.Where(c => c.IsStudent == 1);

Response.Output.WriteLine("Student = 1:<br>");
 

foreach (var cust in result)
Response.Output.WriteLine(cust.Name + " " + cust.Age + "<br>" );

Result:

Student = 1:
Poonam Makati 25
Ayush Patel 7
Yuti Vachhani 12

 

Where - Simple 3


This sample uses where to find all Customers that have contact no and age more than 18 years.

SQL:

SELECT CustID, Name, Sex, Education, IsStudent, Age, ContactNo
FROM Customer
WHERE Age >= 18 AND ContactNo IS NOT NULL

DataClassesDataContext db = new DataClassesDataContext();

LINQ:

var result =
from c in db.Customers
where c.Age >= 18 && c.ContactNo != null
select c;
 
Lambda:

var result = db.Customers.Where(c => c.Age >= 18)
.Where (c => c.ContactNo ! null);
 
Response.Output.WriteLine("Contact No not Blank and Age >= 18:<br>");

foreach (var cust in result)
Response.Output.WriteLine(cust.Name + " " + cust.Age + "<br>" );

Result:

Contact No not Blank and Age >= 18:
Rachit Rokad 30
Poonam Makati 25


==============================================
Shradhdha Zalavadiya |Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

3 comments:

  1. very good article....for beginners..........

    ReplyDelete
  2. This is Abhinav Kumar Singh(abhinavsingh993@gmail.com)I was tried hard to feel the LINQ AND LAMBDA and by getting the glimpse of your website , I got the clear understanding of what is LINQ AND LAMBDA ALL ABOUT, THIS IS CALLED SIMPLE AND GENIUS MIND,
    Thank you Sir

    ReplyDelete