SIGN UP MEMBER LOGIN:    
ARTICLE

Accessing Array and Collection using LINQ

Posted by Kirtan Patel Articles | LINQ with C# April 05, 2010
This article will teach you about how you can Access Array elements and Collections using LINQ.
Reader Level:
Download Files:
 

Introduction

This article will teach you about how you can Access Array elements and Collections using LINQ.

Technology 

CSharp .net 3.5

Implementation

I will teach you with one example of Array and another of List<T> collection. If you are beginner to LINQ, let me teach you basic query format for writing query for LINQ.

from element  in <Collection/Array Name> where <Condition Here> orderby  <SortElementName>  select element

LINQ queries result can be retrieve in IEnumerable<T>

So, we will retrieve result of LINQ query into IEnumerable<T> object.

First I have setup Interface of application like below one 

1.gif

Take one list box to display result of query and two buttons one for showing retrieval from array and one for List<T>

Working with List<T> using LINQ

private void button1_Click(object sender, EventArgs e)

{

   //Working with List<T> Using LINQ

 

   // Creating List

   List<string> list = new List<string>();

   list.Add("orange");

   list.Add("cherry");

   list.Add("banana");

   list.Add("strowberry");

   list.Add("mango");

   list.Add("fig");

 

   //Select Fruites whose String length is less than 3

 

   IEnumerable<string> result = from Str in list where Str.Length > 3 orderby Str.Length select Str;

 

   listBox1.Items.Clear();

   foreach (string s in result)

   {

        listBox1.Items.Add(s);

   }

}

 
Working with Array using LINQ

private void button2_Click(object sender, EventArgs e)

{

    //Creating Array

   string[] StringArray = new string[5];

 

   StringArray[0] = "abc";

   StringArray[1] = "aac";

   StringArray[2] = "abd";

   StringArray[3] = "bad";

   StringArray[4] = "test";

 

   //Select all String Starting with 'a' using LINQ

 

   IEnumerable<string> result = from str in StringArray where str[0] == 'a' orderby str.Length select str;

 

   //Display result in List Box

   listBox1.Items.Clear();

   foreach (string str in result)

   {

        listBox1.Items.Add(str);

   }

}


Conclusion


We have just seen in this article about how to manipulate array and collections using LINQ.

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor