Skip to main content

Brief intro to LINQ

Ive recently given a breif tutorial to the guys on our team on LINQ (Language integrated query)

As with Generics, LINQ is a large subject that i cant write up properly without spending quite a lareg amount of time on, so i'll give a breif overview with an example of a common usage in the context of a generic list.

Overview

LINQ is a technology (or language construct) that allows you to process data using functional programming logic to perform an operation. The LINQ API is an attempt to provide a consistent programming model that allows you to manipulate data. Using LINQ we are able to create directly within the C# programming language entities called query expressions, these query expressions are created from numerous query operators that have been intentionally designed to look and feel similar (but not identical) like SQL. Using LINQ you can interact with many sources of data, listed below:

LINQ implementations
LINQ to objects
Linq to SQL
Linq to entities
Linq to XML
There are other LINQ enabled technologies but these are the most common, for a grater list look online.

All of this means that you can interact with an XML dom in the same manner as you would with a list of objects or an ADO record set without having to learn different syntax for either.

LINQ Example

List names = new List(){"George", "William Smith", "bob", "Fred"};
IEnumerable subSetNames = from n in names
where n.Length >= 4
order by n
select n;
foreach(string name in subSetNames)
{
Console.Write(name);
}

The code above firstly creates a list of strings and places then in the names variable
Then it takes that collection filters it where the length is greater than or equal to 4
Sorts it based on the string
Puts the resulting collection in the variable subSetNames?
Finally it loops through all the strings in subSetNames? and prints them out

The resulting string is "Fred George William Smith" In this example the collection being acted on was a simple list of strings but it could be a collection of any type of object, or indeed an ADO record set, or an XML document, its a very powerful technique and allows for very clear functional logic (no complex nested loops and temporary collections for ordering etc).

This topic is massive and so im not going to go into it in any more depth, but in the most part ive not used any LINQ that is anymore complex than the example listed above. more advanced features such as lambdas and extension methods start to make things complex and ive so far managed to keep clear of using these techniques, for more info search google for the answer.

LINQ Grouping

By way of an example this code below demonstrates the power of LINQ used with a simple grouping command

string[] names = {"Albert", "Burke", "Connor", "David", "Everett", "Frank", "George", "Harris"};

//Defining the GroupBy logic (here it is lengthwise)
var groups = names.GroupBy(s => s.Length);

//Iterate through the collection created by the Grouping
foreach(IGrouping group in groups)
{
//Branch on the condition decided
Console.WriteLine("Strings of Length {0}", group.Key);
//Actual results
foreach(string value in group)
Console.WriteLine(" {0}", value);
}


Produces this output

Strings of Length 6
Albert
Connor
George
Harris
Strings of Length 5
Burke
David
Frank
Strings of Length 7
Everett

LINQ Resources
MSDN 101 LINQ samples

Facebook

Facebook Recommendations

Followers


Web Designing In Karachi



Haroof.com


Politics blogs

My Zimbio

Email Subscribe

Enter your email address:

Watch online Live TV

Popular posts from this blog

Matric General Group Result SECONDARY SCHOOL CERTIFICATE (S. S. C.) PART - II CLASS - X - 2010 (www.apnieyesp.com )

PASSED THE SECONDARY SCHOOL CERTIFICATE (S. S. C.) PART - II CLASS - X) ANNUAL EXAMINATION, 2010. ERRORS AND OMISSIONS EXCEPTED, CANDIDATES BEARING THE FOLLOWING ROLL NUMBERS ARE DECLARED TO HAVE PASSED THE SECONDARY SCHOOL CERTIFICATE (S. S. C.) PART - II CLASS - X) ANNUAL EXAMINATION, 2010. ------------------------------------------------- GENERAL GROUP (REG&PVT) --- GRADE..'A-ONE' ---- ----------------------- ( CANDIDATES SECURING TOTAL MARKS 680 AND ABOVE) MARKS SECURED BY THE CANDIDATES OUT OF TOTAL MARKS OF 850 ARE MENTIONED AGAINST EACH ROLL NUMBER IN BRACKET --------------------------------------------------- 601086 (689) XXX (XXX) XXX (XXX) XXX (XXX) XXX (XXX) XXX (XXX) 601327 (681) 363 (684) 364 (719) 407 (685) 664 (682) 788 (687) 601836 (692) 882 (683) XXX (XXX) XXX (XXX) XXX (XXX) XXX (XXX) 602315 (723) 316 (715) 320 (712) 321 (739) 325 (686) 326 (702) 602327 (683) 329 (70...

Team win: Group Health Insurance

Many small business owners know that to succeed, it must be an incentive for hiring employees who work for them. This can be any number of things, but most of the time, there is the advantage of group health insurance. This is a good strategy for small businesses for hiring new employees, there are several things you should know before you dive in choosing a plan. Before working out insurance for your business. Unable to perform Translation:invalid textA group health insurance plan may be a small business, and as few as two employees to more than fifty. There are two ways to provide health insurance for employees who are on the front line in the budget. Many small and medium enterprises, the group health insurance to contribute to the cost of the plan. On the other hand, if an employee wants to have their families, employers, the payment of staff \ 'you pay the premium and the premium for their families. Unable to perform Translation:invalid textAnother aspect of the health insuran...

Kidnapped student freed in Karachi police raid

Reported by Maryum Razzaq Staff Report KARACHI: Police said that they safely recovered an abducted student from Gulistan-e-Johar here on Sunday. Anti-Violent Crime Cell (AVCL) and Citizens Police Liaison Committee (CPLC) conducted a joint operation in Pehlwan Goth area of Gulistan-e-Johar and recovered Fahad, a student of Karachi University. One kidnapper was killed and his four accomplices managed to escape from the scene. The police said that accused had demanded Rs2 million in ransom to release the student. In another raid, Ferozabad Police apprehended an accused wanted in cases of attempted murders, extortion and kidnapping. SAMAA

Labels

Show more