LINQ: Query vs Lambda Expression

LINQ: Query vs Lambda Expression

  • Comments 8

 

LINQ is not just for the developers who used to write queries for databases but also for the Functional Programmers. So do not worry if you are not very comfortable with SQL kind of queries, we have a very nice option called “Lambda Expression”. Here I am going to demonstrate the scenario for both the options with a small example. This example has an array of integers and I am only retrieving the even numbers using the power of LINQ. Here we go

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Query;

using System.Xml.XLinq;

using System.Data.DLinq;

 

namespace LINQConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arrInt = {1,2,3,4,5,6,7,8,9,10};

 

            #region Place to change

            //Language Integrated Query

            var aa = from s in arrInt

                     where s % 2 == 0

                     select s;

            #endregion

 

            foreach (var item in aa)

            {

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

            }

            Console.ReadKey();

        }

    }

}

 

If you do not want to use the different approach of query for Language then you are free to use Lambda Expression. So just replace the #region area with the following code block results will be identical.

 

            #region Place to change

           

            //Lambda Expression

            var aa = arrInt.Where(s => s % 2 == 0);

 

            #endregion

 

Output will look like

 

Output

=====================

2

4

6

8

10

 

 

Namoskar

 

Leave a Comment
  • Please add 4 and 3 and type the answer here:
  • Post
  • This only works in .NET 3.0 right?

  • This works with .NET 2.0 also. You need to install LINQ for VS 2005 (refer http://blogs.msdn.com/wriju/archive/2006/09/14/753285.aspx) .But LINQ is targeted for .NET Framework 3.5 and Orcas (Visual Studio 2007).

    Wriju

  • Interesting discussion on this topic

    http://programming.reddit.com/info/tzu7/comments

  • It does not only work in .NET 3.0. It works also in .NET 3.5

  • @wriju : Visual Studio 2007?  really?  must have slipped past me, that one. :-)

  • @stimul8d, Bang on.. It should be Visual Studio 2008 :)

  • Hi ,

    can you provide the lambda expression for this

    int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };

       int[] numbersB = { 1, 3, 5, 7, 8 };

       var pairs =

           from a in numbersA

           from b in numbersB

           where a < b

           select new { a, b };

  • Console.WriteLine("Linq Lambda Expression");

               string[] names = {"Vicky","Amar","Bala","Sankar","Vimal" };

               var filter = names.Select(c => new {Namelength=c.Length,Names=c });

               foreach(var v in filter)

               {

                   Console.WriteLine("The Name {0} of Length {1}",v.Names,v.Namelength);

               }

               int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

               var filter1 = numbers.Where(c => c % 2 == 0).Select(c=>c+1).ToList();

               Console.WriteLine(filter1[3]);

               filter1.Insert(4, 13);

               foreach (var v in filter1)

               {

                   Console.WriteLine(v);

               }

               Console.ReadKey();

Page 1 of 1 (8 items)