Solved

Dynamic joins in Linq

Posted on 2010-08-23
4
464 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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