【问题标题】:Using linq to check if not exists clause使用 linq 检查是否不存在子句
【发布时间】:2012-02-09 19:27:36
【问题描述】:

这是我一直试图解决的情况

让我们来一张 Employee 表

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

这里我需要找到所有叶级员工。

叶级员工 - 拥有经理但没有任何人向他们汇报的所有员工。我从具有 TreeLevel 列的 db 获得了一点帮助,我可以在其中指定选择级别 3 的任何人,但我需要一个 UNIONclause 它将让我获得树级别 2 的所有员工,而这些员工没有任何员工报告。 如果这有助于创建 linq 查询,我只有 3 级树。

   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);

更新: 真正的查询:

(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);

错误:

Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1'. 
Only primitive types (such as Int32, String, and Guid) and entity types are supported.

【问题讨论】:

    标签: c# linq-to-sql join union


    【解决方案1】:

    您可以尝试右外连接,并确保左侧为空。

    在这篇 How to do a full outer join in Linq? 的帖子中,您可以找到如何在 linq 中执行此操作的一个很好的示例。

    from b in _db.Employees
    from c in _db.Employees.Where(o=> o.ManagerEmplId == b.Id).DefaultIfEmpty()
    where c == null
    

    【讨论】:

    • 出现如下异常无法比较“System.Collections.Generic.IEnumerable`1”类型的元素。仅支持原始类型(如 Int32、String 和 Guid)和实体类型。
    • 这是我的原始查询,我用通用问题掩盖了我的问题,但这也是简单的返回(从 b in join firm in _db.Branches on b.BranchID 等于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);出错无法比较“System.Collections.Generic.IEnumerable`1”类型的元素。仅支持原始类型(如 Int32、String 和 Guid)和实体类型。
    • return (from b in
      * 加入 _db.Branches 中的公司。BranchID 等于firm.ParentBranchID 进入
      * from sc in g.DefaultIfEmpty()
      * 其中 b.DeletedDate == null && g == null
      * 选择 b).ToList()
      * .ToDictionary(k => k.BranchID, v => v.BranchName);
      * 出现错误
      * 无法比较“System.Collections.Generic.IEnumerable`1”类型的元素。仅支持原始类型(如 Int32、String 和 Guid)和实体类型。
    • g == null 替换为sc == null
    • 我如何使用stackoverflow.com/editing-help#comment-formatting格式化我的 cmets 他们看起来很丑 iam
    【解决方案2】:
    var managersids = _db.Employees.Select(emp => emp.ManagerEmplId ).Distinct();
    var leafemps = _db.Employees.where(emp => !managersids.contains(emp.Employeeid));
    

    要做到这一点很简单,获取所有经理,然后搜索不是经理的人

    var leafemps = from emp in _db.Employees
                   let managersids = _db.Employees.Select(emp => emp.ManagerEmplId ).Distinct()
                   where !managersids.contains(emp.Employeeid)
                   select emp
    

    【讨论】:

    • 当根据建议更改代码时,我收到以下错误此上下文仅支持原始类型('例如 Int32、String 和 Guid')。
    • 好吧,可惜我帮不上忙
    【解决方案3】:

    无论树的深度如何,这个查询都可以解决问题:

    var leafEmps = 
        (from emp in _db.Employees
         where !_db.Employees.Any(e => e.ManagerEmplId == emp.EmployeeId)
         select emp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多