[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dynamic joins in Linq

Posted on 2010-08-23
4
Medium Priority
?
467 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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)…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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