?
Solved

Dynamic joins in Linq

Posted on 2010-08-23
4
Medium Priority
?
466 Views
Last Modified: 2013-11-11
Is it possible to use dynamic joins in Linq where the specific tables that will be joined are not known in advance. We do know what the join conditions will be for all tables, but not the exact tables that will be used. The application gathers user search criteria from forms, then
at that point the tables needed are known. Linq works  for us when the tables needed are known in advance, but when the where condition dictates what tables we need we can't get around this problem of not knowing the tables in advance. There are too many possible tables so we don't want to construct a  massive view that has everything joined up in advance and model that.  Any ideas or examples if this is possible would be appreciated.
Thanks
0
Comment
Question by:awalkinthepark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 2000 total points
ID: 33506710
I've used this technique to build up a where clause dynamically but never to build up joins dynamically, so I don't guarantee it will work, but you could try something like the following. This assumes there is a table you always start with and that the set of tables you might join to are known, you just don't know ahead of time exactly which of them you'll be using. The basic problem here is how do you define the column projection list if you don't know what tables you'll be joining in advance?
var query = context.Table1;

query = query.Join( context.Table2, t1 => t1.PK, t2 => t2.FK, ( t1, t2 ) => t1 );

var results = query2.ToList();

Open in new window

0
 

Author Comment

by:awalkinthepark
ID: 33506752
is it possible to model all the tables then just use the ones we need ad hoc?
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 33507323
I'm not sure I understand the question. That is kind of what the example I posted would do. You would have to wrap some sort of conditional around the second statement to determine whether you wanted to include that join or not.
0
 
LVL 1

Expert Comment

by:devarajamurthy
ID: 33626563
Try to use context.ExecuteQuery() where you can specify dynamically generated  sql query string.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question