C# 从对象列表中查找唯一对象的最简单方法

C# 从对象列表中查找唯一对象的最简单方法,c#,asp.net-web-api,asp.net-core,asp.net-core-2.1,C#,Asp.net Web Api,Asp.net Core,Asp.net Core 2.1,在我的项目中,我有两个实体,第一个是来自客户(或)客户的业务实体(BE),另一个是来自数据库的数据实体(DE) GoodsReceipt.cs public class GoodsReceipt { public Guid Id { get; set; } public string PurchaseOrderId { get; set; } public string Note { get; set; } public v

在我的项目中,我有两个实体,第一个是来自客户(或)客户的
业务实体(BE)
,另一个是来自数据库的
数据实体(DE)

GoodsReceipt.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; }
    }
public class GoodsReceiptProduct
    {
        public Guid Id { get; set; }
        public Guid GoodsReceiptId { get; set; }
        public Guid PurchaseOrderId { get; set; }
        public Guid ProductId { get; set; }
    }
我的要求是在
GoodsReceiptProducts
集合中获取是否添加了
或更新了
或删除了
新项目。GoodsReceiptProduct中的
PurchaseOrderId
对于列表中的所有对象都是唯一的

用户将发送
BE
GoodsReceipt
以及
GoodsReceiptProduct
的集合。那么,从列表中获取该唯一对象的可能方法是什么呢?如果将该列表与服务器上现有的
DE
列表进行比较,该列表可能会被添加、更新或删除。

在IEnumerables中使用
Distinct()

例如:

var YourList=List();
//YourList=在此处分配列表;
var UniqueList=YourList.Distinct();
在IEnumerables中使用
Distinct()

例如:

var YourList=List();
//YourList=在此处分配列表;
var UniqueList=YourList.Distinct();

我想您应该比较两个列表,并确定哪些记录是新添加的、删除的或保留的。而不是找到唯一的对象

为此,您可以使用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);
List existingListInDataEntity=GetExistingList();
List modifiedListInBusinessEntity=GetBusinessList();
var newAddedItems=修改后的ListInBusinessEntity.Exception(现有ListInDataEntity);
var deletedItems=existingListInDataEntity.Exception(modifiedListInBusinessEntity);
var sameItems=modifiedListInBusinessEntity.Intersect(现有ListinDataEntity);

我想您应该比较两个列表,并确定哪些记录是新添加的、删除的或保留的。而不是找到唯一的对象

为此,您可以使用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);
List existingListInDataEntity=GetExistingList();
List modifiedListInBusinessEntity=GetBusinessList();
var newAddedItems=修改后的ListInBusinessEntity.Exception(现有ListInDataEntity);
var deletedItems=existingListInDataEntity.Exception(modifiedListInBusinessEntity);
var sameItems=modifiedListInBusinessEntity.Intersect(现有ListinDataEntity);


确定对象唯一性的标准是什么?@ManojChoudhari PurchaseOrderId是确定对象唯一性的标准。@ManojChoudhari。我想要实现的是在单次输入请求中,我想知道在GoodsReceiptProducts集合中是否添加或删除了一个项目。为什么不维护字典呢。始终将purchaseorderid作为密钥插入。这样,您就可以检查密钥是否已经存在。@ManojChoudhari。是的,我能做到。但我目前的目标是仅使用BE和DE从集合中选择新对象。确定对象唯一的标准是什么?@ManojChoudhari PurchaseOrderId是确定对象唯一的标准。@ManojChoudhari。我想要实现的是在单次输入请求中,我想知道在GoodsReceiptProducts集合中是否添加或删除了一个项目。为什么不维护字典呢。始终将purchaseorderid作为密钥插入。这样,您就可以检查密钥是否已经存在。@ManojChoudhari。是的,我能做到。但我目前的目标是仅使用BE和DE从集合中挑选新的。添加的不止一项是什么?Distinct是否返回那些新添加的项目?Distinct始终返回所有与之不同的项目。您还可以使用软件包
MoreLinq
来使用
DistinctBy
来决定应该使用哪个键来决定某个内容是不同的还是不同的not@BaskaranSDistinct将返回列表的“Distinct”列表项(执行Distinct方法时存在的任何项)。添加的多个项是什么?Distinct是否返回那些新添加的项目?Distinct始终返回所有与之不同的项目。您还可以使用软件包
MoreLinq
来使用
DistinctBy
来决定应该使用哪个键来决定某个内容是不同的还是不同的not@BaskaranSDistinct将返回列表中的“Distinct”列表项(执行Distinct方法时存在的任何项)。@Nilay.这正是我想要的。但是我得到了这个错误=>'List'不包含'Exception'的定义,并且最佳扩展方法重载'ParallelEnumerable。Exception(ParallelQuery,IEnumerable)'需要'ParallelQuery'类型的接收器,因为DE和BE之间不匹配而得到了错误。非常感谢您提供的信息。您需要包括
System.Linq
名称空间。如果两个模型是不同的类型,那么您可以先获得
列表
,然后进行比较Guids@Nilay.This就是我想要的。但是我得到了这个错误=>'List'不包含'Exception'的定义,并且最佳扩展方法重载'ParallelEnumerable。Exception(ParallelQuery,IEnumerable)'需要'ParallelQuery'类型的接收器,因为DE和BE之间不匹配而得到了错误。非常感谢您提供的信息。您需要包括
System.Linq
名称空间。如果这两种模型是不同的类型,那么您可以首先获得
列表
,然后比较该guid