Sealer_05
Sealer_05

Reputation: 5566

linq2sql join select data that is not in another table

Please do not respond using lamba. I have found similar threads but still need help.

I am trying to display the names from the employees table that are not employees of the currently selected manager.

My two sql tables are structured like this but this is fake data

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

So if the the current selected (mgr variable) is 3 I want to get the names of all empPks in the employees table except for 6, 4. (kelly, kip) Right now unselectedEmps = sam, jill, bob instead of all 5 of other names from the employees table.

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;

Upvotes: 0

Views: 1009

Answers (3)

Bert
Bert

Reputation: 82489

After our extended discussion, I think what you want is this.

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

Upvotes: 1

Umesh
Umesh

Reputation: 2732

I have tried the following and it is giving the correct output. Please try it:

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;

Upvotes: 1

cordialgerm
cordialgerm

Reputation: 8503

The problem is that you are doing an inner join when you actually need a left outer join

See this SO question

Upvotes: 1

Related Questions