0

我想加入 SqlKata 中的静态字符串值,但无法弄清楚如何做到这一点。

这就是我的查询需要的方式

LEFT JOIN [tblUserGroupMembers] AS [TUGM] ON ([TUGM].[UserGroupID] = [TUG].[UserGroupId] AND [TUGM].[UserType] = 'Primary')
LEFT JOIN [tblUsers] AS [TU] ON [TU].[UserId] = ISNULL([TIA].[UserId],[TUGM].[UserId])

如您所见,我想加入TUGM.UserType= Primary 和另一个加入 [TU].[UserId] = ISNULL([TIA].[UserId],[TUGM].[UserId])

SqlKata 查询:

var dbQuery = db.Query("tblInvoices AS TI")
            .Join("tblInvoiceActions AS TIA", "TIA.InvoiceID", "TI.InvoiceID")
            .Join("APM_MASTER__INVOICE AS AMI", "AMI.InvoiceID", "TI.InvoiceID")
            .LeftJoin("tblUserGroups AS TUG", "TUG.UserGroupId", "TI.UserGroupId")
            .LeftJoin("tblUserGroupMembers AS TUGM",
                j => j.On("TUGM.UserGroupID", "TUG.UserGroupId")
                .On("TUGM.UserType","['Primary']"))
           .LeftJoin("tblUsers AS TU", "TU.UserId", "[ISNULL(TIA.UserId,TUGM.UserId)]")
           .LeftJoin("tblConnections AS TC", "TC.ConnectionID", "TI.ConnectionID");
4

1 回答 1

1

这些Join方法有一个重载,它在第二个参数中接受一个 lambda,以提供完全的灵活性。使用它WhereRaw来编写原始表达式,当然你应该知道这里的 SQL 注入。

所以你可以在这里使用它:

db.Query("tblInvoices as TI").LeftJoin(
   "tblUsers as TU", 
   j => j.WhereRaw("TU.UserId = ISNULL(...)")
)
于 2019-12-21T17:54:52.010 回答