Part 2: Learning LINQ from SQL using Visual Basic 2008 – WHERE operator

To execute the LINQ statements provided in this post, I suggest you to go through Part-1 (http://jagchat.spaces.live.com/blog/cns!41050F68F010A662!913.entry), where I demonstrated the same.
 

A simple example on WHERE operator of LINQ:

The following SQL SELECT

     SELECT ename, sal FROM emp WHERE sal > 6000

can be coded in LINQ as following:     

Dim query = From p In GetEmployeeList()

Select p.Ename, p.Sal

Where Sal > 6000

Using LIKE with WHERE operator of LINQ:

The following SQL SELECT

     SELECT ename, sal FROM emp WHERE ename LIKE ‘J%’

can be coded in LINQ as following:     

Dim query = From p In GetEmployeeList()

Select p.Ename, p.Sal

            Where Ename Like "J*"

The following SQL SELECT

     SELECT ename, sal FROM emp WHERE ename LIKE ‘W_____’

can be coded in LINQ as following:     

Dim query = From p In GetEmployeeList()

Select p.Ename, p.Sal

            Where Ename Like "W?????"

 

Combined example of SELECT, WHERE and ORDER BY operators in LINQ:

The following SQL SELECT

     SELECT ename, sal FROM emp WHERE deptno = 10 ORDER BY deptno DESC

can be coded in LINQ as following:     

Dim query = From p In GetEmployeeList()

Where p.Deptno = 10

Order By p.Deptno Descending

Select p.Ename, p.Sal

Note: If other operators (WHERE, ORDER BY etc.) are using the columns not listed in SELECT, they need to be moved before SELECT (as above)

About Jag

.NET Architect
This entry was posted in LINQ/EF and tagged , . Bookmark the permalink.