Using LINQ to Objects in C#

| Using LINQ to Objects in C# |
This tutorial was created with Microsoft Visual Studio .NET 2008. However, if you are using 2005, you can implement LINQ by downloading Microsoft's LINQ Community Technology Preview release from here.
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
In this tutorial, we will be looking at using LINQ to Objects. We will be creating a Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example. The label will be to show the results of our functions, and then the other three buttons we will use for LINQ functions.
We will also implement a StatusStrip control to make use of the label within, so that we can manipulate it on the mouse hover and leave events. The form may look something like this:

Once we are done with our form, we can double-click on the buttons in design view to create the click event handlers. We can also create the hover and leave handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
Let's start with the statusstrip label. We will change the text on hover and leave of each of the buttons to let the user know what each of the buttons does:
private void button1_MouseHover(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Display all numbers in array";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
}
private void button3_MouseHover(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Get SUM of all numbers";
}
private void button3_MouseLeave(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
}
private void button2_MouseHover(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Get numbers less than 10";
}
private void button2_MouseLeave(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
}
private void button4_MouseHover(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Get average of numbers";
}
private void button4_MouseLeave(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
} |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Next, we will create a method that will create an array of numbers that we can call from all of the buttons:
private int[] loadNumbers()
{
int[] theNumbers = new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
return theNumbers;
} |
To display all numbers in the array, we use a new keyword in C#, var:
private void button1_Click(object sender, EventArgs e)
{
lblResult.Text = "";
var theResult = from n in loadNumbers() orderby n select n;
foreach(int i in theResult)
lblResult.Text = lblResult.Text + i + " ";
} |
Next, we use a similar structure to select only the numbers from the array that are below or equal to 10. Again, we use LINQ:
private void button2_Click(object sender, EventArgs e)
{
lblResult.Text = "";
var theResult = from n in loadNumbers() where n <= 10 orderby n select n;
foreach(int i in theResult)
lblResult.Text = lblResult.Text + i + " ";
} |
We can also use LINQ built-in functions on the collection, such as the sum of all the numbers and also the average:
private void button3_Click(object sender, EventArgs e)
{
lblResult.Text = "";
var theResult = loadNumbers().Sum();
lblResult.Text = lblResult.Text + theResult.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
lblResult.Text = "";
var theResult = loadNumbers().Average();
lblResult.Text = theResult.ToString();
} |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
|
| Comments |
Debasis Chakraborty said:
delightful usage of LINQ to start gathering knowledge about the brand new feature of .Net 2008
|
Sunil K said:
I have a form containing two DataGridView Controls. say DataGridView1 & DataGridView2.
Northwind Database used.
//Code
DataGridView1.DataSource = OrdBindingSource;
DataGridView2.DataSource=OrdDetailsBindingSource;
how do i sum Column Values of Expression Column (Total Value) of DataGridView2.
I want to reflect the sum into a textbox whenever I select different rown in DataGridView1.
|
Sunil K said:
In Addition this is Master Detail Relationship Between the two Grids and I am Using Untyped DataSet. the Coding is in Visual C#
|
free online casino gambling said:
Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.The techniques used to query against such collections of objects are similar to but simpler than the approaches used to conduct queries against a relational database using SQL statements.
|
Shared Web Hosting said:
I have one problem. I am using static Datacontext Variable. it works fine.. But sometime i got error on website, e.g. Datareader is already open.. then my website goes down for you say 4-5 minutes.. It works fine again..Is this due to connection pooling.Plese suggest me..
|
Essay said:
This blog provides information on how to use LINQ to Objects in C#. This is done by creating a Windows Forms Application which will define an array of numbers and then by means of LINQ to Objects we can interact with this collection of numbers. This is clearly and meticulously explained by means of a diagram. Finally programming part is mentioned so convincingly that there is no room for confusion. Needless to say this is a brilliant blog from a brilliant blogger. The language is simple and technical jargons are avoided as far as possible. Simplicity is the hallmark of this blog. The blogger has succeeded in conveying the information that he wanted to.
|
Gifts said:
Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.
|
Prostate Cancer Treatment said:
We can also create the hover and leave handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
Let's start with the statusstrip label.Thanks
|
Hydraulic jacks said:
This section will discuss some of the common techniques used in LINQ to Objects statement construction. In a nutshell, LINQ to Objects provides the developer with the means to conduct queries against an in-memory collection of objects.
|
Hydraulic jacks said:
The techniques used to query against such collections of objects are similar to but simpler than the approaches used to conduct queries against a relational database using SQL statements.
|
Hydraulic jack said:
This article provides an introduction to employing LINQ to Objects queries to support a simple win forms application; the article addresses the construction of LINQ to Objects statements and then goes on to describe how one might use LINQ to Objects within the context of an actual application.
|
Attorney said:
Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.Thanks
|
Attorney said:
Prostate Cancer Treatment said:
We can also create the hover and leave
handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
Let's start with the statusstrip label.Thanks
|
Fold Up Bikes said:
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.
|
Holiday Costumes said:
I am using static Datacontext Variable. it works fine.. But sometime i got error on website, e.g. Datareader is already open.. then my website goes down for you say 4-5 minutes.. It works fine again..
|
Piano Lessons said:
This article provides an introduction to employing LINQ to Objects queries to support a simple win forms application; the article addresses the construction of LINQ to Objects statements and then goes on to describe how one might use LINQ to Objects within the context of an actual application.
|
Photo Calendars said:
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
|
Live Food said:
This section will discuss some of the common techniques used in LINQ to Objects statement construction. In a nutshell, LINQ to Objects provides the developer with the means to conduct queries against an in-memory collection of objects.
|
simulation assurance auto said:
Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?
|
How to Salsa Dance said:
In Addition this is Master Detail Relationship Between the two Grids and I am Using Untyped DataSet. the Coding is in Visual C#
|
travel said:
Th4t be an epic da shizzi4 post, th4nkie 4it & in da futures we'll be seeing more of it
|
cruises said:
We7ll I8be dat9 ogr6e speekie da speekie, gratz & than4x
|
flight center said:
heb7e sh8at be th34nkie 4it on da posting left & righ8ty
|
Coach outlet said:
heb7e sh8at be th34nkie 4it on da posting left & righ8ty
|
Coach outlet said:
This article provides an introduction to employing LINQ to Objects queries to support a simple win forms application; the article addresses the construction of LINQ to Objects statements and then goes on to describe how one might use LINQ to Objects within the context of an actual application.
|
coach outlet said:
Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.Thanks
|
coach outlet said:
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.
|
louis vuitton bags & lv handbags & louis vuitton & lv said:
else
{
textBox3.Enabled = true;
button4.Text = "Disable >>";
Posted 08/26/2010 at 8:13 AM
|
UGG Bailey Button & UGG Bailey Button Boots said:
handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
Let's start with the statusstrip label.Thanks
|
UGG Bailey Button Triplet said:
handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
|
ugg classic boots & womens ugg boots said:
the MouseHover and MouseLeave events.
|
ugg classic boots & ugg boots said:
the MouseHover and MouseLeave events.
|
ugg classic boots & ugg boots said:
Let's start with the statusstrip label.Thanks
|
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:
textBox3.Enabled = true;
button4.Text = "Disable >>";
Posted 08/26/2010 at 8:13 AM
|
|
|