的同事。
我有一组具有一些共同属性(列)的类(表)。我想要做的是使用EntityFramework拦截选择查询并修改它们。
我想添加join和一些where子句。但是,如果没有明确的属性选择,不知道如何投影左表。
这样的初始查询: context.Loans
SQL
SELECT
[LoanNumber] AS [LoanNumber],
[SEC_OWNER] AS [SEC_OWNER]
FROM [dbo].[Loans]
应修改为以下内容:
SELECT
O.LoanNumber AS LoanNumber,
O.SEC_OWNER AS SEC_OWNER,
FROM dbo.Loans AS O
LEFT OUTER JOIN dbo.Users U
ON
O.SEC_OWNER = U.SEC_ID
AND
U.Name = N'root'
WHERE
((256 = (O.SEC_PERMISSIONS & 256))
AND
(O.SEC_OWNER = U.SEC_ID))
var joinExpression = expression.LeftOuterJoin(
userEntityBase.Scan(),
(l, r) =>
l.Property("SEC_OWNER").Equal(r.Property("SEC_ID"))
.And(r.Property("Name").Equal(DbExpression.FromString(this.UserName)))
)
.Where(
exp =>
DbExpressionBuilder
.Constant((int)AccessPermissions.OwnerRead).Equal(exp.Property("l").Property("SEC_PERMISSIONS").BitwiseAnd(DbExpressionBuilder.Constant((int)AccessPermissions.OwnerRead)))
.And(exp.Property("l").Property("SEC_OWNER").Equal(exp.Property("r").Property("SEC_ID")))
);
据我所知,从两个表中加入项目列。但是我需要在不知道的情况下仅投影初始选择。
SELECT
[LoanNumber] AS [LoanNumber],
[SEC_OWNER] AS [SEC_OWNER]
提前致谢,
/阿尔乔姆
答案 0 :(得分:0)
作为解决该问题的方法,我们从查询中删除了JOIN,并决定使用子选择。
public override DbExpression Visit(DbScanExpression expression) {
...
var userEntityBase = expression.Target.EntityContainer.GetEntitySetByName("User", true);
var newExpression = expression
.Where(
exp =>
...
.And(exp.Property("SEC_OWNER")
.Equal(userEntityBase.Scan().Where(user => user.Property("NAME").Equal(DbExpression.FromString(UserName))).Select(u => u.Property("SEC_ID")).Take(1).Element())
)
)
...
);
...
return base.Visit(expression);
}
。选择(u => u.Property(“SEC_ID”))。Take(1).Element()求值为SELECT TOP 1