Print Page | Close Window

Multiple Model Query

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3227
Printed Date: 28-Mar-2024 at 5:41am


Topic: Multiple Model Query
Posted By: Vincent
Subject: Multiple Model Query
Date Posted: 21-Jan-2012 at 8:56am
In my application I have two models. One model contains customer entity and another contains order entity. These models are coming from different databases. Can you please give me a sample linq query of how I can retrieve particulars of an order showing its customer name?
 
 
Best Regards,
Vincent



Replies:
Posted By: sbelini
Date Posted: 23-Jan-2012 at 4:25pm
Hi Vincent,
 
While you can have an EntityManager with multiple models pointing to different datasources, you can only query one database at the time. (You can, however, save multiple entities to multiple databases in one single transaction)
 
So, you will need to first retrieve the customers and then query for the orders. (or vice versa)
 
i.e
 
var aCustomer = mgr.Customers.First();
var aCustomerOrders = mgr.Orders.Where(ord => ord.CustomerID == aCustomer.Id)
  .ToList();
 
or
 
var myOrder = mgr.Orders.First();
var CustomerName = mgr.Customers.Where(cust => cust.Id == myOrder.CustomerID)
  .Select(cust => cust.CompanyName)
  .Distinct();
 
You might want to take a look at the http://drc.ideablade.com/xwiki/bin/view/Documentation/many-models-per-manager - DevForce Resource Center for more information.
I've also attached a simple solution showing one EntityManager with multiple models. ( http://www.ideablade.com/forum/uploads/892/T11435.zip - uploads/892/T11435.zip )
 
 
Regards,
   Silvio.



Print Page | Close Window