Solved

Dynamic joins in Linq

Posted on 2010-08-23
4
458 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now