WPF Reflections

Feb 26 2008   8:25PM GMT

Creating an ObservableCollection from Linq for Sql

Mark Shurmer Profile: MarkWPF

Just how do you create an ObservableCollection from a Linq query?
Not easily is the answer.

If you are using WCF as part of your multi-tier strategy, then you can employ a trick that converts your generated Linq for Sql EntitySet classes into ObservableCollection classes. See a previous post of mine for details.

If you are not using WCF, then like me you’re a bit stuck – you seem to have to do it manually. For each access you need to convert it to a non Linq business object class, and crucially all it’s properties that contain lists.
I have here an example from the AdventureWorks database where the Products List is populated from the database into a ProductModel class, and I show how to do this with a couple of sub properties and one sub list (ProductCostHistories).

First here is the Product class, which is a separate class from the Linq generated Product class:

Product class

Here is the ProductModel class which does the interaction with Linq:

ProductModel class

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: