Lambda Expression


Lambda Expressions are based on function programming; they have been added to .NET. A Lambda Expression is essentially a very compressed function often used where a delegate would be used, and they have a precise syntax, which is roughly:


Function(arg1, arg2...argn) expression

You can explicitly type the parameters—that is, provide the argument types or leave them unspecified and the compiler will figure it out. The expression part is a statement such as a + b, or s.Length. The Return keyword is implicit.


Expression Tree

An expression tree is the Lambda Expression broken up into explorable chunks (or nodes) that describe expression. Common members of the Expression class are Body, NodeType, Type, and Parameters.


The Body is the statement that makes up the expression. The NodeType is an instance of the enumeration ExpressionType that can contain values such Lambda, Divide, Not, Power, and much more. The Expression.Type is the underling type of the Lambda Expression mapped to a generic delegate like Func(Of String, Integer). The Parameters property is a collection of ParameterExpression objects that contain information about the parameter type and name .


Example:









using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;




namespace ConsoleApplication1

{

      class Program

       {

        [STAThread]

         static void Main(string[] args)

         {




            string[] names = { "Ajay", "Rajesh", "Shubhankar",

           "Ravi", "Preeti", "Tulika",

           "Anup", "Saurabh" };


           IEnumerable<string> expr = from s in names

            where s.Length < 8

            orderby s

            select s.ToUpper();


            foreach (string item in expr)

            Console.WriteLine(item);

            string[] names1 = { "Ajay", "Rajesh", "Shubhankar",

             "Ravi", "Preeti", "Tulika",

            "Anup", "Saurabh" };


             // group by length

             var groups = names1.GroupBy(s => s.Length);


             foreach (IGrouping<int, string> group in groups)

              {

              Console.WriteLine("Strings of length {0}", group.Key);


              foreach (string value in group)

                Console.WriteLine(" {0}", value);

               }


               Console.ReadKey();

         }

    }

}


 



Output:


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2013 VisualBuilder. All rights reserved