Solved

Dynamic joins in Linq

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 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