LINQ (Language Intergrated Query)

LINQ is a new type-safe query structure available in C# 3.0 and .NET 3.5 Microsoft framework.  With LINQ you can query against any collection that implements IEnumerable<> or remote data sources.  With this, LINQ benefits from compile-time checking and dymanic query composition.  To use LINQ include the following namespaces from System.Core assembly:

System.Linq
System.Linq.Expressions

LINQ consists of 2 basic data units, sequences and elements.  A sequence is defined as any object implementing the IEnumerable and elements are any item within the sequence.

To transform a sequence you use a method called query operator, which typical accept an input sequence and produce an output sequence.  The IEnumerable class in LINQ has around 40 or so query operators that are refered to as standard query operators.

A standard query expression would look as follows:

C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] query = { "I", "Was", "Here" };  //String collection to query
            IEnumerable<string> filteredQuery = query.Where(n => n.Length >= 1);  //query with lambda expression.
            foreach (string q in filteredQuery)
                Console.Write(q + " ");
        }
    }
} //Console output: I Was Here

Leave a Reply