4

Here is situation I have been trying to solve

Lets take a Employee table

Create Table Employee
(
        Employeeid int primary key,
        EMPname varchar(50),
        ManagerEmplId int reference key Employee (EmployeeID)
         TreeLevel int,
              ....
)

Here i need to find all leaf level employees.

Leaf Level Employees - All employees who have manager but they do not have anybody reporting to them. I have a little help from db which has TreeLevel column where I can specify pick anybody at level 3 but I need a UNIONclause which will get me all employees at treelevel 2 that do not have any employees reporting. I have only 3 levels of tree if that helps in creating linq query.

   return ((from b in _db.Employees
                && b.TreeLevel==3 && b.DeletedDate== null
                    select b)
                    .Union
                    (from b in _db.Employees

                     select b)

                    )
                    .ToDictionary(k => k.EmployeeID, v => v.EMPname);

UPDATE: The real query:

(from fi in firm 
 join bra in _db.Branches on fi.BranchID equals bra.ParentBranchID into g 
 from sc in g.DefaultIfEmpty() 
 where fi.DeletedDate == null && g == null 
 select fi)
 .ToList()
 .ToDictionary(k => k.BranchID, v => v.BranchName);

Error:

Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1'. 
Only primitive types (such as Int32, String, and Guid) and entity types are supported.
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
user1147971
  • 55
  • 1
  • 5

3 Answers3

1

This query should do the trick, regardless of tree depth:

var leafEmps = 
    (from emp in _db.Employees
     where !_db.Employees.Any(e => e.ManagerEmplId == emp.EmployeeId)
     select emp);
Nuffin
  • 3,882
  • 18
  • 34
1

You can try the right outer join and make sure that the left side is empty.

In this post How to do a full outer join in Linq? you can find a good example of how to do it in linq.

from b in _db.Employees
from c in _db.Employees.Where(o=> o.ManagerEmplId == b.Id).DefaultIfEmpty()
where c == null
Community
  • 1
  • 1
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • getting exception as follow Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported. – user1147971 Jan 13 '12 at 17:56
  • Here is my original query I masked my issue withgeneric one but this one is also simple return (from b in join firm in _db.Branches on b.BranchID equals firm.ParentBranchID into g from sc in g.DefaultIfEmpty() wher b.DeletedDate == null && g == null select b).ToList() .ToDictionary(k => k.BranchID, v => v.BranchName); Getting err Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported. – user1147971 Jan 13 '12 at 18:01
  • return (from b in
    * join firm in _db.Branches on b.BranchID equals firm.ParentBranchID into
    * from sc in g.DefaultIfEmpty()
    * wher b.DeletedDate == null && g == null
    * select b).ToList()
    * .ToDictionary(k => k.BranchID, v => v.BranchName);
    * Getting error
    * Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.
    – user1147971 Jan 13 '12 at 18:07
  • replace the `g == null` with `sc == null`. – Jahan Zinedine Jan 13 '12 at 18:09
  • how do i format my comments they are looking ugly iam using http://stackoverflow.com/editing-help#comment-formatting – user1147971 Jan 13 '12 at 18:11
0
var managersids = _db.Employees.Select(emp => emp.ManagerEmplId ).Distinct();
var leafemps = _db.Employees.where(emp => !managersids.contains(emp.Employeeid));

To do this simple, get all the managers and then search the people who arn't a manager

var leafemps = from emp in _db.Employees
               let managersids = _db.Employees.Select(emp => emp.ManagerEmplId ).Distinct()
               where !managersids.contains(emp.Employeeid)
               select emp
Frederiek
  • 1,605
  • 2
  • 17
  • 32