Josh-CO Dev

Solving the worlds problems one line of code at a time.

Developer Learning Program – LINQ

Leave a comment


Well, today marks the launch of our Developer Learning Program. My colleague and I have chose LINQ for our first topic and he will be giving a quick presentation and providing a real-time demo through Live Meeting. It will be given to our team, plus some management and our architect. With any luck, things will go smoothly and we can continue these training sessions every two weeks.

Here is some information my colleague wrote up as a handout:

LINQ is used to run queries against data sources such as lists, collections, relational data bases, xml, and entities (business objects) to name a few. The .NET objects representing the data sources must implement the IEnumerable<T> interface (or any derived interfaces such as IQueryable<T>) in order to run a LINQ query against them.

 

Key Programming Concepts desired to use LINQ proficiently:

  • SQL Syntax – .NET Generics – .NET Extension Methods – .NET Lambda Expressions & Anonymous Functions

 

CurrentOrders = List<Order> 
OrderId Customer DateEntered
1 ABC-Manufacturing 06/15/2010
2 Prime Time-Studios 09/29/2011
3 ABC-Manufacturing 01/25/2011

 

CurrentOrderDetails = List<OrderDetail>
OrderDetailId OrderId Product
9000 1 iPad 2
9101 2 2TB Hard drive
9102 3 Monitor

 

Exercise 1: Select OrderId “2” from the list.

Option 1:where” Clause

var order = (from ord in CurrentOrders where ord.OrderId == 2 select ord).FirstOrDefault();

var listOfOrders = (from ord in CurrentOrders where ord.OrderId == 2 select ord).ToList();

 

Option 2:Where” Extension with Lambda expression

var order = CurrentOrders.Where(ord => ord.OrderId == 2).FirstOrDefault();

 

Note: “ord” refers to the current order while enumerating the list as it does in a foreach loop.

foreach (Order ord in CurrentOrders) {

   if(ord.OrderId == 2) {…};

}

 

Exercise 2: If list is a datatable instead, return the rows where the customer is ABC-Manufacturing

Option 1: Projection with Anonymous type

var rows = (from ord in CurrentOrders.AsEnumerable()

                  where ord.Field<string>(“Customer”) == “ABC-Manufacturing”

                   select new {OrderId = ord.Field<int>(“OrderId”),

                                        Customer = ord.Field<string>(“Customer”),

                                        DateEntered = ord.Field<DateTime>(“DateEntered”)});

 

Exercise 3: Return the Customer, Product, & DateEntered order for customer Prime Time -Studios

Option 1: Projection with “join” clause and Anonymous type

var orders = (from ord in CurrentOrders

                          join d in CurrentOrderDetails on ord.OrderId equals d.OrderId

                     where ord.OrderId = 2

                      select new {ord.Customer, d.Product, ord.DateEntered});

 

Good Sites:

The Basics: http://msdn.microsoft.com/en-us/library/bb397906.aspx

101 LINQ Samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s