Dynamic query creation (dynamic search engine)

495 Views Asked by At

We are using ASP.NET MVC3, EF 4.3.1, DynamicQuery library We are currently developping a dynamic search engine that could perform search query agains multiple database.

So we have an interface that allows the user to create a query. Here is what the interface looks like this : screenshot with selected ddl value

So after that, we recreate the hierarchy of groups / clauses inside the controller and then we have to perform some joins between different tables (for example : Study / People).

The problem is that when we perform the joins, we do them as follow :

if (queries.ContainsKey("Drug"))
{
    query = query.Join(GetContext().StudyDrugs, s => s, sd => sd.StudyId, (s, sd) => new { Study = s, StudyDrug = sd })
                 .Join((IQueryable<int>)queries["Drug"], obj => obj.StudyDrug.DrugId, d => d, (obj, d) => obj.Study);
}

But then we can't set the Where clauses of the generated query since they don't always apply to the searched IQueryable<T> but on the joined objects.

And : Since we need to be able to group clauses (that why we use groups in the screenshot), we have to be able to generate the Where clause correctly with the parenthesis. (this could be done with the PredicateBuilder class it seems but since the result of the search is an IQueryable<T>, we don't know if the where clause applies to the T class or one of the joined class.

Am I missing something ? Is there any tricks that could allow me to add the where clause after the joins have be done but before the object are pulled from the database (still on the IQueryable object) ?

EDIT :

In fact, if I was able to specify the 'as' name in the generated SQL query, the ending where clause could be done pretty easily. Is there any ways to name the joined table (T2 in the following example)?

SELECT * FROM Table1 T1
JOIN Table2 T2 on (T1.T2Key = T2.Key)
0

There are 0 best solutions below