Wednesday, April 29, 2015

LINQ simple example, and diff between QUERY and METHOD syntax




           // 1. Pre-LINQ way of doing it:
            // *******************************************

            foreach (var c in customerList)
            {
                if (c.CustomerId == customerId)
                {
                    foundCustomer = c;
                    break;
                }
            }


            // 2. LINQ using Query Syntax:
            //*******************************************
         
         var query = from c in customerList
                        where c.CustomerId == customerId
                        select c;

            foundCustomer = query.First();




            // 3. LINQ using METHOD Syntax:
            //*******************************************

            foundCustomer = customerList.First(c =>
                c.CustomerId == customerId);

     



Thank you, Deborah Kurata!

No comments: