Programmer's Heaven

Programmer's Heaven
Picture

Linq To Object

Hi,







Here i have submitted the LINQ--> Object Simple programming for the Beginner's for this concept.


LINQ to Objects provides the developer with the means to conduct queries against an in-memory collection of objects. 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.



First of all, we have to import the following things,


using System; using System.Collections.Generic; using System.Linq; using System.Text;
A Simple Select Query
string[] articles = { "C++", "VB", "JAVA", ".NET", "SQL", "LINQ" };
var list = from t in articles select t;
StringBuilder sb = new StringBuilder();
foreach (string s in list) { sb.Append(s + Environment.NewLine);
} MessageBox.Show(sb.ToString(), "Articles");

In the example, an array of strings (tools) is used as the collections objects to be queries using LINQ to Objects; the LINQ to,
Using Object
Objects query is: var list = from t in articles select t;

In this example, an untyped variable "list" is created and all of the items contained in the string array are added to this object; the types are inferred (implicitly typed), for example, "t" is a member of tools, since it is known that tools is a string array, the framework will infer that "t" is also a string. Of course this is not all that terrific since you can just iterate through the array to do essentially the same thing; however, you can create more complex queries with LINQ to Objects and then the value of the LINQ library becomes more apparent.

Select Query With Where Clause

The next example shows a LINQ to Objects query that incorporates a where clause. In this example, we start out with a collection of birds in the form of a string array; LINQ to Objects is used to query this string array to find and return a subset of the array in the form of all birds with names beginning with the letter "R".

string[] Birds = { "Indigo Bunting", "Rose Breasted Grosbeak", "Robin", "House Finch", "Gold Finch",
 "Ruby Throated Hummingbird","Rufous Hummingbird", "Downy Woodpecker" };
var list = from b in Birds where b.StartsWith("R") select b;
 StringBuilder sb = new StringBuilder();
foreach (string s in list) { sb.Append(s + Environment.NewLine);
 } MessageBox.Show(sb.ToString(), "R Birds");

Let see, How to generate an order list,
Generating an Ordered List

string[] Birds = { "Indigo Bunting", "Rose Breasted Grosbeak", "Robin", "House Finch",
 "Gold Finch", "Ruby Throated Hummingbird","Rufous Hummingbird", "Downy Woodpecker" };
var list = from b in Birds orderby b ascending select b; //this is place where we selecting the order StringBuilder sb = new StringBuilder();
foreach (string s in list) { sb.Append(s + Environment.NewLine);
} MessageBox.Show(sb.ToString(), "Alphabetized Birds");

We can also a Search a Column with the Value Contains or StartsWith or EndsWith etc.,
Search a Values or List

var matchingParts = from m in parts where m.PartDescription.StartsWith("S") select m;//The list which startswith 'S' StringBuilder sb = new StringBuilder(); foreach (Parts p in matchingParts)
{ sb.Append(p.PartNumber + ": " + p.PartDescription + Environment.NewLine);
} MessageBox.Show(sb.ToString(), "Matching Parts List");