0

我正在尝试在我的 C# 代码中创建一个带有条件的左连接。我可以在 SQL Server 中编写它,但在将其编写为 lambda 表达式时遇到问题。这是我的 SQL 代码的粗略部分:

Select x.RequestId, aud.DepartmentId
From Requests x
Left Join UserDepartment ud on x.AssignedToTeam = ud.DepartmentId and ud.User = 'Joe'

我知道如何编写左连接,但不熟悉如何或是否可以添加条件。

4

3 回答 3

3

怀疑你想要这样的东西:

var query = from request in db.Requests
            join department in db.UserDepartments
                                 .Where(dep => dep.User == "Joe")
              on request.AssignedToTeam equals department.DepartmentId
              into departments
            from dep in departments.DefaultIfEmpty()
            select new { request.RequestId,
                         DepartmentId = dep == null ? null : (int?) dep.DepartmentId
            };

(如果需要,显然更改您想要处理缺少部门 ID 的方式。)

于 2012-10-10T19:03:17.093 回答
0
var request = db.View.Join(
    db.UserDepartment.Where(w=>w.User=="Joe"),
    a=>a.AssignedToTeam, 
    b=>b.DepartmentId, 
    (a,b)=> new {View = a, UserDepartment = b});
于 2012-10-10T23:47:17.550 回答
0

查看 MSDN 文章:如何:执行左外连接(C# 编程指南)

于 2012-10-10T19:01:36.310 回答