A second step in my simple WCF Web service using Entity Framework
Couple days ago I started a simple application (WCF Web service using Entity Framework to access a SQL DB and a WPF client).
In the SQL database I had 3 tables - mainly a many-to-many relation.
Now I need to get more data: I want to obtain all the Products and for each Product to get the details : from ProductInventory (which is related to Product) and from Location (which is related to ProductInventory) tables.
On the server side I am using twice the Include method and that's it ! So simple and clean !
///
/// get all products ordered by name
///
/// if getWithDetails=true get the details from the 2 related tables
///
public List GetProducts(bool getWithDetails)
{
try
{
ObjectQuery prodList = this.productionContext.Products;
return (getWithDetails) ? prodList.Include("ProductInventory").Include("ProductInventory.Location").OrderBy("it.Name").ToList()
: prodList.OrderBy("it.Price").ToList();
}
catch (InvalidOperationException ioex)
{
throw ioex;
}
catch (Exception ex)
{
throw ex;
}
}
You can see how it looks on the client side.
And below you can donwload the source code.
Comments
- Anonymous
February 16, 2009
This is the next step in my previous sample (see the previous post from this month). Now I want to add