Monday, February 11, 2013

Linq - Delegates , Anonymous functions




delegate in where condition


An anonymous function is an "inline" statement or expression that can be used wherever a delegate type is expected. You can use it to initialize a named delegate or pass it instead of a named delegate type as a method parameter.

var fu = productMemberResponse.ProductList.Where(delegate(ProductMemberDetails pmd)
                {
                    if (pmd.Type == "SEP")
                        return sepProduction;
                    else if (pmd.Type == "PIM")
                        return pimProduction;
                    else return true;
                }
                ) ;



A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.


(input parameters) => expression

(input parameters) => {statement;}

Func<bool, int> del = x => x ? 10 : 5; 
    new ShoppingCart().Process(del);


No comments:

Post a Comment