Print Page | Close Window

Multiple Field Join

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4255
Printed Date: 03-May-2025 at 6:46pm


Topic: Multiple Field Join
Posted By: JaxGuyWestside
Subject: Multiple Field Join
Date Posted: 30-Jul-2013 at 7:55am
Hello,

I'm trying to write an DevForce LINQ query where I need to join on multiple fields.  I looked in the documentation and found the examples for joins, but I didn't see an example that addressed this specific scenario.  I searched the web for linq queries with multiple field joins and here is what I found which does convey what I'm trying to do; I just need to figure out how to do it for DevForce.

from x in entity
join y in entity2 on new { x.field1, x.field2 } equals new { y.field1, y.field2 }

Any help is greatly appreciated!!
JD



Replies:
Posted By: kimj
Date Posted: 30-Jul-2013 at 12:01pm
You should first read this - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/linq-join-examples - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/linq-join-examples  - to determine whether you really do need a join.  In most models, relationships can be expressed with navigation properties, for example:
  aCustomer.Orders
  anOrder.Customer
 
If you do need to join entities which don't have an existing foreign key relationship, you can do so like you've done above.  For example, here's a contrived example to join Northwind Employees and Users based on their names:
 
var query = from e in entityManager.Employees
             join u in entityManager.Users
             on new { id1 = e.FirstName, id2 = e.LastName } equals new { id1 = u.FirstName, id2 = u.LastName }
             select e;
var results = query.ToList();
 
The types and names of properties in the anonymous types used in the join must be the same, and here the "id1" and "id2" names aren't required.
 
 
 
 
 


Posted By: JaxGuyWestside
Date Posted: 31-Jul-2013 at 7:28pm
Thank you for the quick response.  I believe that's basically what I was doing; but I must have had a syntax error of some sort that was causing the problem.  At least I had the right concept.

Thanks again!
JD



Print Page | Close Window