LINQ LINQ defines a set of standard query operators that allow traversal, filter, and projection operations to be expressed directly in any .NET-based language The standard query operators allow queries to be applied to any IEnumerable<T>-based information source(e.g arrays and collections). In the following example we'll begin with a simple C# program that uses the standard query operators to process the contents of an array: namespace ConsoleApplication1 query =from s in names where s.ToLower().StartsWith("a") orderby s select s; Next is from statement that always starts a query. It takes a collection of some kind after the "in" keyword and makes what is to the left of the "in" keyword refer to a single element of the collection Following this is another new keyword: where This introduces a filter, allowing us to pick only some of the objects from the Orders collection OrderBy works usait like its SQL counterpart to sort the result. The final new keyword is select.It states the result we want to appear in query.
LINQ is shorthand for Language Integragted Query. If you have previous experience in SQL, you are going to have a head start in LINQ. Today a big challenge in programming technology is to reduce the complexity of accessing and integrating information that is not defined using OO technology.
{
class Program
{
static void
{
//Declare a string array
string[] names = { "Ashish", "James", "Albert"};
var query = from s in names
where s.ToLower().StartsWith("a")
orderby s
select s;
//Following will output Ashish,Albert as they startrs with "a"
foreach (string item in query)
Console.WriteLine(item);
Console.ReadLine();
}
}
}
The variable query is initialized with a query expression. A query expression operates on one or more information sources by applying one or more query operators from either the standard query operators or domain-specific operators. This expression uses three of the standard query operators: Where, OrderBy, and Select.
Lets go through the above Query expression one by one.
We declare the variable query and request that its type be inferred for us by using the "var" keywordIEnumerable<string> query = names .Where(s => s.StartsWith("a")) .OrderBy(s => s) .Select(s => s.ToUpper());
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

fatih adiguzelPosted Mar 17, 2011, 6:58 AM
it is plain and well built. thanks for sharing
Mahendra RahatePosted Nov 27, 2009, 5:47 AM
Your article on LINQ was very easy to follow. Please post a complete tutorial on LINQ & LAMBDA for beginers like me.