New Posts New Posts RSS Feed: Life without joins?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Life without joins?

 Post Reply Post Reply
Author
darrelmiller View Drop Down
Newbie
Newbie


Joined: 16-Dec-2011
Posts: 3
Post Options Post Options   Quote darrelmiller Quote  Post ReplyReply Direct Link To This Post Topic: Life without joins?
    Posted: 21-Dec-2011 at 6:13am
In this documentation page it is suggested that we should be able to avoid joins 
to create a more natural looking request.  I have run into a case where I don't 
see how to do this without joins.

Consider your example of customers and orders.  Say I wanted to create a list 
of orders and their corresponding sales region where the sales region was a 
property of customer.

So, the join syntax would be 

query   = from o in manager.Orders
                  join  c in manager.Customers on o.CustomerId = c.Id
                  join  sr in manager.SalesRegions on c.SalesRegionId = sr.Id
          select new { OrderNumber = o.Number, SalesRegion = sr.Name };

results = query.ToList() 


From the examples in your documentation I wasn't able to see how to create a query that projects from more than one entity in the join, 
so I was in the process of telling you how it could not be done and I found out how.... So rather than throwing this email away, I'll post
what I think is a solution and maybe someone can tell me if I'm on the right track.

With method chaining,

query = manager.SalesRegions
        .SelectMany(sr => sr.Customers)
        .SelectMany(c => c.Orders)
        .Select(o => new {OrderNumber = o.Number, 
SalesRegion = o.Customer.SalesRegion.Name});




Edited by darrelmiller - 21-Dec-2011 at 6:14am
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: 21-Mar-2012 at 11:30am
Hi darrelmiller,
 
Sorry for the (very) late reply.
 
You could also try: (based on NorthwindIBEntities)
 
var query = mgr.Orders
               .Select(ord =>new {orderNumber = ord.OrderID,
                                  salesPersonManager = ord.Employee.Employee2.FirstName});
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down