0

我想将 LINQ 转换为 SQL 到 SQL 查询。我知道简单的 LINQ to SQL,但不知道更多。

下面给出的是我的 Linq to Sql,我想将其转换为 Sql 查询。

from objSql in objContext.DoctorNotes
join objCreatedU in objContext.Users on objSql.CreatedByUserFK equals objCreatedU.UserID into objCU
from tblC in objCU.DefaultIfEmpty()
join objModifiedU in objContext.Users on tblC.ModifiedByUserFK equals objModifiedU.UserID into objMU
from tblM in objMU.DefaultIfEmpty()
select new DoctorNoteBind(objSql)
{
    CreatedBy = tblC.UserName,
    ModifiedBy = tblM.UserName
}).ToList();
4

1 回答 1

0

.DefaultIfEmpty() 用于表示LEFT JOINs ( http://msdn.microsoft.com/en-us/library/ms187518(v=sql.100).aspx )。您希望等效的 SQL 采用以下形式:

SELECT
    U1.UserName AS CreatedBy,
    U2.UserName AS ModifiedBy
FROM
    DoctorNotes DN
LEFT JOIN Users U1 ON
    U1.UserID = DN.CreatedByUserFK
LEFT JOIN Users U2 ON
    U2.UserID = DN.ModifiedByUserFK
-- WHERE ?
-- ORDER BY ?
于 2013-07-01T05:18:46.860 回答