c# - 从对象列表中查找唯一对象的最简单方法
问题描述
在我的项目中,我有两个实体,第一个是Business entity(BE)
来自客户(或)客户,另一个Data Entity(DE)
是来自数据库。
收货.cs
public class GoodsReceipt
{
public Guid Id { get; set; }
public string PurchaseOrderId { get; set; }
public string Note { get; set; }
public virtual ICollection<GoodsReceiptProduct> GoodsReceiptProducts { get; set; }
}
GoodsReceiptProduct.cs
public class GoodsReceiptProduct
{
public Guid Id { get; set; }
public Guid GoodsReceiptId { get; set; }
public Guid PurchaseOrderId { get; set; }
public Guid ProductId { get; set; }
}
我的要求是获取新项目是否在集合中added
或集合updated
中。而在 GoodsReceiptProduct 中对于列表中的所有对象来说都是唯一的。deleted
GoodsReceiptProducts
PurchaseOrderId
用户将BE
GoodsReceipt
连同一组GoodsReceiptProduct
. 那么从列表中获取该唯一对象DE
的可能方法是什么,在将该列表与服务器上的现有列表进行比较时,该对象可能会被添加、更新或删除。
解决方案
我猜您想比较 2 个列表并确定哪些记录是新添加的、删除的或保持不变的。而不是找到一个独特的对象。
为此,您可以使用except & Intersect
例如:
List<GoodsReceiptProduct> existingListInDataEntity = GetExistingList();
List<GoodsReceiptProduct> modifiedListInBusinessEntity = GetBusinessList();
var newAddedItems = modifiedListInBusinessEntity.Except(existingListInDataEntity);
var deletedItems = existingListInDataEntity.Except(modifiedListInBusinessEntity);
var sameItems = modifiedListInBusinessEntity.Intersect(existingListInDataEntity);
推荐阅读
- python - 从循环生成的 python tk.entry 获取输入时出现问题?
- python - Django,如何在一页中添加两个表单?
- python - 使子弹移动到光标
- python - 如何在 Windows 上使用 python -c?
- javascript - 是否可以将单元格引用用作 getRange() 中的行/列位置?
- javascript - 如何为 API 中缺少图像的对象生成随机图像
- python - Python如何合并具有多列的两个数据框,同时保留每列中的行顺序?
- javascript - JS中的VBA刮表
- microsoft-graph-api - 为什么我没有收到有关共享日历事件更改的通知?
- rest - 通过 REST API 添加新销售订单时出错 - Acumatica