Print Page | Close Window

Life without joins?

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=3174
Printed Date: 08-May-2025 at 7:53am


Topic: Life without joins?
Posted By: darrelmiller
Subject: Life without joins?
Date Posted: 21-Dec-2011 at 6:13am
In  http://drc.ideablade.com/xwiki/bin/view/Documentation/linq-join-examples - 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});





Replies:
Posted By: sbelini
Date 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.



Print Page | Close Window