c# - 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; }
}
解决方案
试试这个:
DataGrid.ItemsSource = Emps.Where(emp=> Emps.Exists(itm=> itm.Mgr.HasValue && emp.Empno == itm.Empno));
推荐阅读
- php - 将值(包括空格)传递到 PHP 中的另一个页面
- apache - 连接到 docker 容器时连接重置
- python - 如何在 Python 中增加字母?
- c# - 如何通过 app.config 禁用实体框架数据库初始化?
- amazon-ec2 - ec2 主机名静态 ip 指向公共 dns,而不是静态 ip 后缀警告
- c++ - 用于将笛卡尔 (x,y,z) 转换为 2D/3D 圆柱 (ρ,θ,z) 坐标的代码
- r - 为什么我的函数在控制台中逐步工作时将“字符(0)”作为输出
- memory - Rust 如何在运行时存储类型?
- sql-server - 用于提取 SQL Server 元数据的 PostgreSQL TDS_FDW 连接
- uitableview - 在 iOS 13 上构建时,UITableViewCell selectedBackgroundView 的颜色不可见