Craig’s Blog

March 11, 2009

It’s The Small Things

Filed under: .Net — cpope @ 10:27 am

I just spent way too long debugging a Windows service, only to find out the code I was debugging was no longer even used. It had been pulled out to a separate class, but when it was the original code (which happened to be where my breakpoints were) was left behind.

All that to say when you refactor, delete stuff you’re not using. Make decluttering part of what you do. It keeps code clean, and makes eveyone’s life (incuding yours) that much simpler.

January 14, 2009

Entity Framework & Left Outer Joins

Filed under: .Net, Entity Framework — cpope @ 9:23 am

So I was racking my brain trying to figure out how to do left outer joins in the Entity Framework.  Several blog posts had some ideas, when suddenly I realized a very simple solution.  Simply query the entity you’re interested in, include the joined entity, and then check the count on the join.  The assumption is that you have an association between the two, which you should anyway.  Here are a few examples. 

 
// returns a count of all dogs
int i = context.Dogs.Include(“DogsFedToday”)
.Count(); 

// returns a count of all dogs not fed today
int j = context.Dogs.Include(“DogsFedToday”)
.Where(x => x.DogsFedToday.Count == 0)
.Count(); 

// returns a count of all dogs fed today
int k = context.Dogs.Include(“DogsFedToday”)
.Where(x => x.DogsFedToday.Count > 0)
.Count();
 

If you’ve got other ways that are less taxing, more efficeint, or just easier, I’m all ears.

January 10, 2009

Entity Framework Inheritance and Associations

Filed under: .Net, Entity Framework — cpope @ 5:09 am

If you can’t tell, I like to post things that either (1) I think might help out someone else or (2) I know I’ll want to know how to do again in the future.  This post is definitely a (2).

I’ve been working with Microsoft’s Entity Framework since beta, and must say it’s one of the greatest developer productivity enhancers I’ve seen in a long time.  Today I was working on inheriting from an entity and then associating that inherited entity.  After several failed attempts on my own, I found a great tutorial addressing just this.

Thanks, Moses.

January 7, 2009

Datasets in Silverlight

Filed under: .Net, Silverlight, WCF — cpope @ 1:21 am

I’ve been working on a project porting ASP.Net code to Silverlight.  In the old code, we had a dashboard widget to query data and return the results in a grid.  It was trivial to do this with datasets – just query the data, return the dataset, then bind it to a GridView or DataGrid.  The columns and rows were all automatically generated.

I’m fairly new to WCF and Silverlight, but quickly realized that WCF wanted a contract.  That didn’t exactly fit our dynamic model.  We tried returning the query results via WCF as an XML string, and then parsing the string into XML in Silverlight and binding to a DataGrid, but the results were not always formatted  like we expected.

Finally someone on our team found a post in the Silverlight forums that was exactly what we were looking for.  They basically create a few custom classes and then pull them into Silverlight as an ObservableCollection.  Since it took us so long to find it, I thought I’d post it here in hopes that it helps someone else out down the road.

January 4, 2009

Welcome, 2k9

Filed under: humor — cpope @ 3:24 am

I told myself I’d start blogging this year, so here goes.  I saw this little ditty while out grabbing pizza with the family tonight.

pizza

Yeah, that’s how my since of humor is.  Welcome to my world.

Blog at WordPress.com.