Lambda expressions and Func signatures

The standard query operators utilize generic Func delegates.

Func<TSource,bool> matches a TSource=>bool lambda expression: one that accepts a TSource argument and returns a bool value.

Similarly, Func<TSource,TResult> matches a TSource=>TResult lambda expression.

The signature of the Select query operator:

 
public static IEnumerable<TResult> Select<TSource,TResult> 
              (this IEnumerable<TSource> source, Func<TSource,TResult> selector)
  

Func<TSource,TResult> matches a TSource=>TResult lambda expression: one that maps an input element to an output element.

TSource and TResult are different types.

The following query uses Select to transform string type elements to integer type elements:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        string[] names = { "C", "Java", "C#", "Javascript" };

        IEnumerable<int> query = names.Select(n => n.Length);

        foreach (int length in query)
            Console.Write(length + "|");  
    }
}
  

The output:


1|4|2|10|

Here is the Where query operator method signature.

 
public static IEnumerable<TSource> Where<TSource>
   (this IEnumerable<TSource> source, Func<TSource,bool> predicate)
  

The Take operator outputs the first x elements, discarding the rest:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] numbers = { 10, 9, 8, 7, 6 };

        IEnumerable<int> firstThree = numbers.Take(3);
        
        foreach (int i in firstThree){
            Console.WriteLine(i);        
        }
    }
}
  

The output:


10
9
8

The Skip operator ignores the first x elements and outputs the rest:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] numbers = { 10, 9, 8, 7, 6 };
        IEnumerable<int> lastTwo = numbers.Skip(3); // { 7, 6 }
        foreach (int i in lastTwo){
            Console.WriteLine(i);        
        }
    }
}

The output:


7
6

First, Last, ElementAt

Not all query operators return a sequence.

The element operators extract one element from the input sequence.

First, Last, and ElementAt all return a single element


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] numbers = { 10, 9, 8, 7, 6 };
        int firstNumber = numbers.First();  // 10
        int lastNumber = numbers.Last();  // 6
        int secondNumber = numbers.ElementAt(1);  // 9
        int lowestNumber = numbers.OrderBy(n => n).First(); // 6
    }
}

Aggregation operators

The Aggregation operators return a scalar value; usually of numeric type:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] numbers = { 10, 9, 8, 7, 6 };
        int count = numbers.Count();  // 5;
        Console.WriteLine(count);
        int min = numbers.Min();  // 6;
        Console.WriteLine(min);
    }
}

The output:


5
6

The quantifiers return a bool value:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] numbers = { 10, 9, 8, 7, 6 };
        bool hasTheNumberNine = numbers.Contains(9);  // true bool 
        bool hasMoreThanZeroElements = numbers.Any(); // true bool 
        bool hasAnOddElement = numbers.Any(n => n % 2 == 1);  // true
    }
}

Concat and Union

Some query operators accept two input sequences.

Concat appends one sequence to another

Union does the same but with duplicates removed:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] seq1 = { 1, 2, 3 };
        int[] seq2 = { 3, 4, 5 };

        IEnumerable<int> concat = seq1.Concat(seq2);  //  { 1, 2, 3, 3, 4, 5 } 
        
        IEnumerable<int> union = seq1.Union(seq2);  //  { 1, 2, 3, 4, 5 }
    }
}
  

In query syntax

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        string[] names = { "C", "Java", "C#", "Javascript" };

        IEnumerable<string> query =
        from n in names
        where n.Contains("a")   // Filter elements 
        orderby n.Length        // Sort elements
        select n.ToUpper();     // Translate each element (project)

        foreach (string name in query) 
           Console.WriteLine(name);
    }
}
  

The output:


JAVA
JAVASCRIPT
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.