Lambda Expressions and Linq Operators

 
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 = names
        .Where(n => n.Contains("a"))
        .OrderBy(n => n.Length)
        .Select(n => n.ToUpper());

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

The output:


JAVA
JAVASCRIPT

In example above, we are using the following lambda expression to the Where operator:


n => n.Contains ("a")

The input Input type is string, and the return type is bool.

The lambda expression depends on the particular query operator.

The Where operator tells whether an element should be included in the output sequence.

The lambda expression maps each element to its sorting key in the OrderBy operator.

The lambda expression determines how each element is transformed in the Select operator.

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.