1

我有一个由动态 linq 生成的查询。问题是当我自加入初始数据集时,所有其他过滤器都在最新表上。我希望它是最初的。我怎样才能做到这一点?这是一个例子:

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

然后我想过滤一个参数的初始数据

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

如果我没有内连接,则查询被正确过滤,但使用内连接,它会对初始类型的最新查询进行过滤,并且选择位于最新表上。生成的查询是这样的:

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) 

但我希望它是:

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) 
4

1 回答 1

0

问题出在这一行

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

它应该是

report = report.Join(subQuery, propertyName, propertyName, "outer");
于 2012-12-10T14:32:31.773 回答