17
2011
Writing Your First LINQ Program. .
LINQ (Language Integrated Query) is a methodology that simplifies and unifies the implementation of any kind of data access. LINQ does not force you to use a specific architecture; it facilitates the implementation of several existing architectures for accessing data. As with every tool, it can be used in both good and in bad ways. To get the most out of LINQ, you will have to master it.
Today, data managed by a program can belong to different data domains: an array, an object graph, an XML document, a database, a text file, a registry key, an e-mail message, Simple Object Access Protocol (SOAP) message content, a Microsoft Office Excel file…. The list is long.
I will guide you through writing a first LINQ Program in C#.Net.
I hope,you can create new console application in Visual Studio. After you have created new console application in Console Application. On Program.cs or Program.vb file, type the following code inside Main function.
string[] greetings = { "Hello World", "Hello LINQ", "Hello PRince" };
var items = from s in greetings where s.EndsWith("LINQ") select s;
foreach (var item in items)
Console.WriteLine(item);
Console.ReadLine();
Since you saw, we can search an array just like you use Database Query. First Line declares Array of string.
Look at the second line, it’s as simple as human language. Don’t wonder, .Net still supoorts var variable type. var can hold data of any type.Okay, Now lets look at the exact query; It takes all the string which End with “LINQ” and store them in variable items.
Foreach reads all values stored in items (Which is collection of String which have “LINQ” on last) as loop.
Console.WriteLine(item); is used to display each string in Console screen.
Having trouble on Typing and Executing Query?
1. On Visual Studio, Go to File Menu.
2. From New, select New Projet. On the Project Dialog, Select Language as Visual C# and project type as Console Application.
3. Give Name/Solution Name as First and click Ok.
4. Open Program.cs file and clear all the lines from Program.cs
5. Copy the code below and enjoy your first LINQ Program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace First
{
class Program
{
static void Main(string[] args)
{
string[] greetings = { "Hello World", "Hello LINQ", "Hello PRince" };
var items = from s in greetings where s.EndsWith("LINQ") select s;
foreach (var item in items)
Console.WriteLine(item);
Console.ReadLine();
}
}
}
Happy Coding ! ! !

An article by
ur website is nice !! and about LINQ is good……keep it up…. and keep in touch.
gr8 work.keep going
[...] LINQ myself. Before starting detail LINQ tutorial did you check my last Post about LINQ? It not Check it here. Well, no more talks, let’s start our next tutorial to search xml file content using [...]