c# - LINQ 选择列表中的所有孤儿
问题描述
我正在使用 C# 中的以下模型
public class Bill
{
public Guid Id { get; set; }
public Document Document { get; set; }
}
public class Document
{
public Guid Id { get; set; }
public List<Payment> Payments { get; set; }
}
public class Payment
{
public Guid Id { get; set; }
}
我想Payments
使用 LINQ 选择所有没有父母的人。
我从几个 SO 答案中了解到以下技术,如下所示:
List<Payment> paymentList = db.Payments
.Where(p => !db.Documents.Any(d => d.Payment.Id == p.Id))
但如果Document
只包含一个Payment
,而不是整个列表,则此方法有效。
您知道如何调整查询以检索所有孤立的付款吗?
解决方案
// All the payments whose id is not contained in any of all the
//Documents.Payments
List<Payment> paymentList = db.Payments
.Where(p => db.Documents.SelectMany(d => d.Payments)
.All(p2 => p2.Id != p.Id));
推荐阅读
- reactjs - 在 react-router-dom 中输入错误并使用打字稿做出反应
- math - 我急需解决这个问题
- groovy - 将闭包作为参数传递给 Jenkins Groovy 中的另一个闭包
- stata - 使用 Tobit 和 Hurdle 模型估计需求函数
- c++ - Qt:如何让 MainWindow 访问其子项的数据
- python-3.x - 窗口之间的PyQt5关系,首先不会关闭
- c# - 无法将“System.DateTime”类型的对象转换为“System.String”类型”错误
- python - 以视觉方式表示大量数据的最佳方式是什么?
- php - 为什么我在 Laravel 中使用 storage::files 得到空数组?
- c# - Blazor - 使用 INT ID 的级联下拉列表不填充数据