Search This Blog

Monday, November 25, 2013

LINQ Query Expressions C# example

C# > LINQ Query Expressions

Language-Integrated Query (LINQ) is the name for a set of technologies of .NET Framework. This component adds data query  capabilities directly into the C# language.
LINQ expressions are like SQL statements  and can be used to extract and process data from arrays, classes, XML and databases.

Data Source - Query Expression - Execute

Methods
Examples

Example 1.  Fundamental query concepts

            // data source
            int[] int_array = new int[] { 5, 4, 2, 8, 10 };

            // query expression
            IEnumerable<int> intQuery =
                from int_v in int_array
                where int_v >= 5
                select int_v;

            // Execute the query
            foreach (int i in intQuery)
            {
                MessageBox.Show(i.ToString());
            } 
    
Example 2. Order by query

            IEnumerable<int> intQuery =

                from int_v in int_array
                where int_v >= 5
                orderby int_v ascending
                select int_v;

Example 3.
Implement RANK function


Example 4. IsNumeric

Example 5. Order By Key Dictionary