1
Answer

How do LINQ query in C#?

Bhuvana

Bhuvana

12y
1.3k
1
How do LINQ query in C#? Pls explain briefly with example
Answers (1)
0
Senthilkumar

Senthilkumar

NA 15.2k 2.4m 12y
H Bhuvana,

A query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language. Different languages have been developed over time for the various types of data sources, for example SQL for relational databases and XQuery for XML. Therefore, developers have had to learn a new query language for each type of data source or data format that they must support. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.

All LINQ query operations consist of three distinct actions:

Obtain the data source.

Create the query.

Execute the query.

For example,

// The Three Parts of a LINQ Query:
        //  1. Data source.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQuery)
        {
            Console.Write("{0,1} ", num);
        }

For more info:
http://msdn.microsoft.com/en-us/library/bb397906.aspx 
http://www.dreamincode.net/forums/topic/72464-using-linq-in-c%23-part-i/ 

and also you can see recommended articles in right side.


Accepted