New Posts New Posts RSS Feed: Multiple Model Query
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Forum LockedMultiple Model Query

 Post Reply Post Reply
Author
Vincent View Drop Down
Newbie
Newbie


Joined: 21-May-2010
Location: Tanzania
Posts: 22
Post Options Post Options   Quote Vincent Quote  Post ReplyReply Direct Link To This Post Topic: Multiple Model Query
    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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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 DevForce Resource Center for more information.
I've also attached a simple solution showing one EntityManager with multiple models. (uploads/892/T11435.zip)
 
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down