Josh-CO Dev

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


Leave a comment

Developer Learning Program – LINQ

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 comment

LINQ in the Business Database World

I have come across two dilemmas lately in my professional career, the first of which is that I love LINQ. The second dilemma is that the way we do business does not provide us with much of a need for it. Really, these two aren’t dilemmas on their own, but combine the two and the problems begin to arise. All of our apps are generally just a front end to a database. We fetch data via a dataset and use this for all databinding and pass all CRUD operations through a webservice to call stored procs. With this type of a model, LINQ just isn’t that efficient unless you want to pull all of your data at once and then parse through your collection, which can be cumbersome on the initial load for a large table.

There is one place where I have found good use for LINQ, in batch jobs where I am pulling data from one source and posting it to another, particularly if I need to scour the data. Below is some LINQ code that I have used to take a dataset that I have pulled in from a source database, scour out any rows that have a null or <0 value, then return the results in a datatable. I tried this many other ways and LINQ was by far the fastest. This operation will run on over 100,000 records in mere seconds.

 

private DataTable scourDatasetLINQ(DataSet ds)

{

try

{

var results = from myRow in ds.Tables[“PIData”].AsEnumerable()

where myRow.Field<double?>(“value”) != null && myRow.Field<double?>(“value”) >= 0

select myRow;

 

DataTable dt = results.CopyToDataTable<DataRow>();

return dt;

}

catch (Exception ex)

{

LogExceptionToFile(“CEMSBatch”, ex, 30);

return null;

}

}


Leave a comment

Some LinqToSQL Samples to get you through the day

So, one of the things that I am learning from scratch with Silverlight is LinqToSQL. In my professional life, we are still using typed datasets to do all of our databinding and our restricted to WinForms and ASP.net. So this is all new to me. I have found the process to be incredibly simple and thought I would share some code.

Click past the jump for code.

Continue reading