Product cross sells using Linq to SQL

Ok, so finally I have an e-commerce related post.  Tonight I was adding cross-sells to my site.  The concept is simple - when displaying a product, at the bottom of the product page, list products that might also be of interest to the shopper.  To facilitate this, you'll need a Cross Sell table:

This table just contains two product id's.  One is the product Id of the item the shopper is currently viewing, the other is the product Id of the product that you want to cross sell.  So all you have to do is make a call out to your database by passing your product Id and return one or more products that you want to cross sell.  Then display them.

In the past, I would have written a stored procedure to retrieve the data.  Something like this: 

But I'm moving away from stored procs in this new e-commerce site I'm working on, and using Linq to Sql.  I have written hundreds on basic linq queries, but how do you accomplish this task?  I have a select embedded inside a select.  Well, here is the solution:

First grab your Cross Sell records and store them in a var called crossSells.  Then create another query from your Products table, join to manufacturers (to get the manuf. name for dispay purposes), and set the where to get those items from crossSells that contain your productId.  No matter how many "queries" you construct, only one SQL statement is executed.   Your var is an IQueryable and you can embed these inside others.  Very cool and very easy to get straight in your head when you seperate them. 

 

 

Your SQL query isn't actually constructed and run until you enumerate over it (call .ToList() for example).  In the above code, I want to return a generic list of Product objects. So, after creating my two linq statements, I return products.ToList();

The SQL that is created can be easily seen.  Just set a breakpoint on the return statement and look products. 

There you have it.  Move away from stored procedures and into the cool world on Linq to SQL (or Linq to Entities).

Thanks to Rob Conery's blog for this information.

Making table changes in SQL Server 2008

This is more of a note to myself.  Every time I reinstall SQL 2008 and want to make a table change, I am faced with this error: 

Nice warning, but please, just let me make changes to my tables.  I'm trying to develop here.  Now I understand this is good for a production environment in case you are making a quick change to a table (I would use RedGate's tools myself though).

To fix this, in SSMS, go into the Tools menu, choose Options.  Then click on Designers in the treevier.  Uncheck "Prevent saving changes that require table re-creation".  There you have it. 

Again, I have forgotten about this setting a dozen times.  Hope it helps someone.

Silverlight 3 Pros and Cons

great post by Dan Wahlin here on the pros and cons of SL3 developement.  I have been wanting to blog more on my experiences in e-commerce development but am struggling to find time.  I'm a one man shop at the moment!  Work on my new ASP.NET site is coming along nicely.  I have a professional UI in place now, just tidying up the paging and completing the checkout process.  Once that is finished, It's back to my SL3 administration application.  Silverlight is such a great platform and it's really going to allow my customers to efficiently manage their store. 

More blogging to come...

AG_E_PARSER_BAD_PROPERTY_VALUE

Why in Silverlight 3 do I still get these cryptic error messages?  This happens quite frequently to me.  I do a lot of copy/paste between my user controls in Blend or Visual Studio.  Take a look at this XAML for a Button: 

 

<Button x:Name="btnSave" HorizontalAlignment="Right" Margin="5" Content="Save" Grid.Column="3" Grid.Row="20" Click="btnSave_Click" />

 

I just copied this from another user control onto a new user control.  It just happens to contain a handler for the Click event, "btnSave_Click".  Well if you forget to create the event handler for it in your new control, you end up with the cryptic

 

To fix, go back to your XAML, right click on this:

Click="btnSave_Click"

and choose "Navigate to Event Handler".  This will create a stub method for you and you can continue on.

Why in a V3 release are we still not helping developers along?  Yeah, I goofed by just doing a copy/paste without also creating the event handler, but why isn't VS smart enough to tell me this?