为了账号安全,请及时绑定邮箱和手机立即绑定

从对象列表中找到唯一对象的最简单方法

从对象列表中找到唯一对象的最简单方法

C#
ITMISS 2022-10-23 15:06:05
在我的项目中,我有两个实体,第一个是Business entity(BE)来自客户端(或)客户,另一个Data Entity(DE)是来自数据库。收货.cspublic 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.cspublic 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 中对于列表中的所有对象来说都是唯一的。deletedGoodsReceiptProductsPurchaseOrderId用户将BE GoodsReceipt连同一组GoodsReceiptProduct. 那么从列表中获取该唯一对象DE的可能方法是什么,在将该列表与服务器上的现有列表进行比较时,该对象可能会被添加、更新或删除。
查看完整描述

2 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

我猜您想比较 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);


查看完整回答
反对 回复 2022-10-23
?
慕森王

TA贡献1777条经验 获得超3个赞

Distinct()在 IEnumerables 中使用


例如:


var YourList = List<GoodsReceiptProduct>();


//YourList = propulate the list here;


var UniqueList = YourList.Distinct();


查看完整回答
反对 回复 2022-10-23
  • 2 回答
  • 0 关注
  • 54 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信