Reader Level:

Select Items with Where in LINQ and C#

By Mahesh Chand on Jun 21, 2007
This article shows how to select exact match and in between items from a collection using LINQ and C#.

This code is written using LINQ Preview May 2006 CTP.


To run this sample code, you must have Visual Studio 2005 Express or other Visual Studio 2005 versions.


After that, install LINQ Preview May 2006 CTP version from MSDN website.


After installation of LINQ Preview May 2006 CTP, click on New Project in the File menu of Visual Studio 2005, select LINQ Preview in the Project Types and create a LINQ Console Application from the selected templates.


If you want to write your sample code in VB.NET, Select Visual Basic in the Project types and LINQ in the Templates pane.


See Figure 1.





Figure 1.


As name suggests, LINQ (.NET Integrated Query Language) provides syntax to select data from collections.




The syntax in LINQ is similar to SELECT..FROM..WHERE clause of SQL. Here is a simple

from day in daysArray
select day;

In the above statement, I select day in daysArray. It returns an object of "var" type, which is a generic type and can store any type. For example, daysArray can be an array of numbers, strings, or even objects. Here is an example of daysArray.

string[] daysArray = { "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };

This is how return value is stored from a statement.

var days = from day in daysArray select day;

Now where clause is used to filter the results. For example, the following syntax gets retuslts from daysArray where value of day is "Wed" only.

var days = from day in daysArray where day =="Wed" select day;


Filtering using where


The following code selects items from an array that have value below 20.


int[] IntArray = { 12, 5, 24, 10, 9, 8, 4, 87, 23, 7, 11, 43 };

            var under20 = from u20 in IntArray where u20 < 20 select u20;

            Console.WriteLine("Numbers below 20 are: ");


            foreach (var numU20 in under20)







The output looks like Figure 2.


Figure 2.


The following query is used to get items between two values, greater than 8 and less than 20.


var under20 = from u20 in IntArray where u20 < 20 && u20 > 8 select u20;


The output looks like Figure 3.


Figure 3.


The exact match items can be retrieved using == operator. For example, the following syntax selects items that have value 23.


var under20 = from u20 in IntArray where u20 == 23 select u20;


The output looks like Figure 4.


Figure 4.



Check out More Fun with LINQ Technology by Mike Gold



Mahesh Chand
Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more



Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Infragistics jQuery Controls

Trending up