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.


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

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

Why not to use the “delete before publishing feature” in Visual Studio

So, I learned an important lesson yesterday, I narrowly avoided wiping the entire site so far. Not that I have a whole lot of content yet, but it would have been a bit heartbreaking to say the least. I am working on a personal project that involves setting up a subdomain on this site that will host a silverlight application. My web project is set to delete all files and replace them when I perform a publish. Well, I am troubleshooting some issues with a WCF service and posted some files in my root directory. A couple of mistakes later and I accidently tried to publish my project to the root drive rather than the folder that houses my subdomain. Many of my folders and all of my files were wiped from the root directory. Thankfully, I happened to have a backup of most of the files and most all wordpress content is housed in the database. Talk about a rough lesson to learn!


Leave a comment

.NET – Querying data from a binding source

Rather than write code that is chatty with the database, it is possible to pull all the data that your user could ever need from a table or procedure and store it in a dataset. Normally you would then drop in a binding source to link up your datagrid and the dataset. In some cases, you may want to search the data in the grid to find a particular piece of data. Maybe you want to give your users the ability to enter a value in a search box and navigate directly to that record. Using the binding source, this is as simple as two lines of code.

Here it is:

int itemFound = bindingSource.Find(”Field”, Convert.ToInt32 (ValueToFind));

bindingSource.Position = itemFound;


Leave a comment

.NET – Filtering on a BindingSource

So here is another cool trick that I picked up recently. It is incredibly simple to filter on a binding source. Using this code, you can pull all the data back into a DataGrid, other other tool, and then let the users filter the data using the binding source. Thus, you can delimit the data without making subsequent calls to the database.

Here’s the code:

BingingSource.Filter = “FieldName= “Variable + “AND FieldName2= ” + Variable2;