Josh-CO Dev

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


Leave a comment

Developer Learning Program – First Meeting

Apparently our first meeting instituting the developer learning program was a huge success. We gave a quick overview on LINQ and presented a live demo where we wrote a linq query, did some conditional statements, a few joins, and showed off some of the LINQ extensions. It was well received by both developers and management and really seemed to open up some eyes. Be it for better or worse, this may get us too much attention!

 

The only issues we really ran into is that it is really a change in thinking for our devs. We do things the old way. We pass all db calls through a webservice and into a data layer where stored procs are called and returned via a dataset. Honestly, LINQ is near useless in this type of environment. We explained how this methodology is pretty “chatty” and will result in slower response times. Unless you are posting data frequently or have a real time app, you can generally pull larger sets of data one time and then filter the data via LINQ. This is where it will fit in with our organization.

Advertisements


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


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

Developer Learning Programs

At my place of employment we tend to do things the old fashioned .NET way, think datasets. We have an outdated framework and an outdated knowledge-base. One of our biggest problems is that many of our developers simply do not keep up with current development trends, they tend to become comfortable in their jobs and not want to learn anything else voluntarily. To help combat this, we are beginning a developer learning program where we spend about 30 minutes every other week giving a presentation and demo on many newer topics. I will try to post some of this information here as we share it at work. Here is a list of our beginning topics:

Named Parameters

Optional Parameters

Dynamic keyword

Automatic properties

Anonymous type

Lambda

Linq  

XML

Metro

Metro style apps vs classic style apps

XAML

Silverlight and WPF

RIA

Inheritance

Interface development

XML Documenter

Debugging


Leave a comment

C# Nullable datatypes – How to set a variable to accept nulls

This is a nifty little trick that I almost forgot about until today. Often times you need to pass a null value into a non-nullable datatype, such as an int. If you don’t know a trick, this can be pretty difficult. Here’s the trick:

int? varName;

Simple eh? Just add a question mark to the end of the datatype. This will force it to become a nullable datatype.


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


Leave a comment

Silverlight WCF RIA Issues – Implement Domain Service

As i’ve mentioned in previous posts, I have been having some issues with WCF services in Silverlight. On my local computer, my application would compile and run just fine. When I would move the web service out to my host, GoDaddy, I would always get back errors that did not make any sense. I scoured the internet and found nothing of use. I have a clientaccesspolicy.xml, crossdomainpolicy.xml and did everything else the web told me. I then stumbled into some articles about using the entity framework with RIA calls, I am using LinqToSQL. These articles uses a domain services file to interface with the web service. I implemented this and everything began working. The weird thing is, so far it seems that I only need to expose the table itself through the domain service. I can still make all my web service calls, inserts, etc by calling my web service directly. Most frustrating problem ever!