Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0374 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.