Wednesday, March 16, 2011

LINQ : Language Integrated Query - A Simple Example

class Order

{

    private int _OrderID;

    private int _CustomerID;

    private double _Cost;

    public int OrderID

    {

        get { return _OrderID; }

        set { _OrderID = value; }

    }

    public int CustomerID

    {

        get { return _CustomerID; }

        set { _CustomerID = value; }

    }

    public double Cost

    {

        get { return _Cost; }

        set { _Cost = value; }

    }

}

 

class Program

{

    static void Main(string[] args)

    {

        // Set up some test orders.

        var Orders = new List<Order> {

                         new Order {

                             OrderID = 1,

                             CustomerID = 84,

                             Cost = 159.12

                         },

                         new Order {

                             OrderID = 2,

                             CustomerID = 7,

                             Cost = 18.50

                         },

                         new Order {

                             OrderID = 3,

                             CustomerID = 84,

                             Cost = 2.89

                         }

                     };

        // Linq query.

        var Found = from o in Orders

                    where o.CustomerID == 84

                    select o.Cost;

       

        // Display results.

        foreach (var Result in Found)

            Console.WriteLine("Cost: " + Result.ToString());

    }

}

 

 

 

The output of running this program is:

Cost: 159.12

Cost: 2.89

No comments:

Post a Comment