How to filter dynamic linq query on the initial data when there is self-join

c# dynamic-linq

Question

I have a query that I generate by dynamic linq. The problem is when I self-join the initial data set, all other filters are on the latest table. I want it to be on the initial. How can I do that? Here is an example:

var result = dataContext.Person;
var initialData = result;
....
var subQuery = initialData.Where(String.Format(predicateEntityIn, entityPropertyName), subSelection);
report = report.Join(subQuery, propertyName, propertyName, "inner");
... 

Then I want to filter the initial data on a parameter

report = report.Where(String.Format("{0} = {1}", "PersonId", paramValue));

If I don't have the inner join then the query is correctly filtered, but with the inner join it does the filtering on the latest query of the initial type and also the select is on the latest table. The query that is generated is something like that:

SELECT DISTINCT [t2].[Field1], [t2].[PersonId], [t2].[Field2]
FROM [dbo].[PersonDetails] AS [t0]
INNER JOIN [dbo].[Person] AS [t1] ON [t1].[PersonId] = [t0].[PersonId]
INNER JOIN [dbo].[PersonDetails] AS [t2] ON [t0].[PersonId] = [t2].[PersonId]
WHERE ([t2].[YearOfBirth] = 2012) 

But I want it to be:

SELECT DISTINCT [t0].[Field1], [t0].[PersonId], [t0].[Field2]
FROM [dbo].[PersonDetails] AS [t0]
INNER JOIN [dbo].[Person] AS [t1] ON [t1].[PersonId] = [t0].[PersonId]
INNER JOIN [dbo].[PersonDetails] AS [t2] ON [t0].[PersonId] = [t2].[PersonId]
WHERE ([t0].[YearOfBirth] = 2012) 
1
1
12/6/2012 1:56:59 PM

Accepted Answer

The problem was in this line

report = report.Join(subQuery, propertyName, propertyName, "inner");

it should be

report = report.Join(subQuery, propertyName, propertyName, "outer");
0
12/10/2012 2:32:31 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow