0

请不要使用lamba回复。我找到了类似的线程,但仍然需要帮助。

我正在尝试显示员工表中不是当前所选经理的员工的名称。

我的两个 sql 表的结构是这样的,但这是假数据

Employees:   
           pk      name     
           1      bob
           2      sam
           3      greg
           4      kip
           5      jill
           6      kelly
           7      chris


ExpenseTeamMembers: 

           pk      expMgrPk     empPk     
           1         7          2
           2         7          5
           3         7          1
           4         3          6
           5         3          4

因此,如果当前选择的(mgr 变量)是 3,我想获取员工表中除 6、4 之外的所有 empPks 的名称。(kelly,kip)现在 unselectedEmps = sam,jill,bob 而不是所有 5雇员表中的其他名称。

var unselectedEmps = (from u in db.employees
                      join o in db.expenseTeamMembers on u.pk equals o.empPk
                      where o.expMgrPk != mgr 
                      select u.name).ToList();

                lstAvailable.DataSource = unselectedEmps;
4

3 回答 3

1

问题是当您实际上需要左外连接时,您正在执行内连接

看到这个问题

于 2012-07-25T02:29:22.583 回答
1

经过我们的扩展讨论,我想你想要的是这个。

from u in db.Employees
where !(from e in db.ExpenseTeamMembers
        where e.expMgrPk == selectedMgr.pk
        select e.empPk).Contains(u.pk)
select u.Name
于 2012-07-25T03:33:48.300 回答
1

我尝试了以下方法,它给出了正确的输出。请试一试:

List<Employees> emps = new List<Employees>();
        emps.Add(new Employees { PK = 1, Name = "bob" });
        emps.Add(new Employees { PK = 2, Name = "sam" });
        emps.Add(new Employees { PK = 3, Name = "greg" });
        emps.Add(new Employees { PK = 4, Name = "kip" });
        emps.Add(new Employees { PK = 5, Name = "jill" });
        emps.Add(new Employees { PK = 6, Name = "kelly" });
        emps.Add(new Employees { PK = 7, Name = "chris" });

        List<ExpenseTeamMembers> etm = new List<ExpenseTeamMembers>();
        etm.Add(new ExpenseTeamMembers { empPK = 2, ExpMgrPK = 7, PK = 1 });
        etm.Add(new ExpenseTeamMembers { empPK = 5, ExpMgrPK = 7, PK = 2 });
        etm.Add(new ExpenseTeamMembers { empPK = 1, ExpMgrPK = 7, PK = 3 });
        etm.Add(new ExpenseTeamMembers { empPK = 6, ExpMgrPK = 3, PK = 4 });
        etm.Add(new ExpenseTeamMembers { empPK = 4, ExpMgrPK = 3, PK = 5 });

        var query = from t in
                        (
                            from emp in emps
                            join o in etm on emp.PK equals o.empPK into j
                            from k in j.DefaultIfEmpty()
                            select new { Name = k == null ? string.Empty : emp.Name })
                    where t.Name != string.Empty
                    select t.Name;
于 2012-07-25T04:34:35.307 回答