Home » Programming » LINQ » Story Details
Printable Version

LINQ select items with Where in C#

by kimi kalon on Sep 28, 2011

This article shows how to select exact match and in between items from a collection using LINQ and C#.
Download Files: linq.zip
Comments: 0    Views: 614

This article shows how to select exact match and in between items from a collection using LINQ and C#. Now open Visual Studio 2010 click on New Project in the File menu of Visual Studio 2010, select C# with console application from the selected templates.

 

Note

If you want to write your sample code in VB.NET, Select other Language -> Visual Basic
with console application in the Project types in the Templates pane.

 

See Figure 1.

 

WhereLinqImg1.gif 

Figure 1.

 

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

 

Understanding from..where..select

 

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

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 from..select 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: ");

            Console.WriteLine("=====================");

            foreach (var numU20 in under20)

            {

                Console.WriteLine(numU20);

            }

            Console.WriteLine("======================");

            Console.ReadLine();

 

The output looks like Figure 2.

 

WhereLinqImg2.gif 

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.

 

WhereLinqImg3.gif

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.

 

WhereLinqImg4.gif 

Figure 4.

Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName