首页 > 解决方案 > Linq lamda EXISTS() 查找具有特定属性的对象

问题描述

我需要在 lamda 表达式中找到所有具有 Exists() 方法的下属的员工。我完全不知道要这样做。

首先,我试图找到有老板(经理)的员工:

DataGrid.ItemsSource = Emps.Where(emp => Emps.Exists(m => m.Mgr != null));

但它不起作用,甚至没有意义。

我无法修改列表、Emp 类、xaml,也无法添加新类。

public List<Emp> Emps { get; set; }
public class Emp
{
    public int Empno { get; set; }
    public string Ename { get; set; }
    public int Sal { get; set; }
    public int? Comm { get; set; }
    public int Deptno { get; set; }
    public int? Mgr { get; set; }
    public DateTime HireDate { get; set; }
    public string Job { get; set; }
}

标签: c#linq

解决方案


试试这个:

DataGrid.ItemsSource = Emps.Where(emp=> Emps.Exists(itm=> itm.Mgr.HasValue && emp.Empno == itm.Empno));

推荐阅读