1

In my project, I have two entities first one is the Business entity(BE) which is coming from a client (or) customer and another one is Data Entity(DE) which is from the database.

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; }
    }

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; }
    }

My requirement is to get whether a new item is added or updated or deleted in the GoodsReceiptProducts collection. And the PurchaseOrderId in GoodsReceiptProduct is a unique one for all the object in the list.

The user will send the BE GoodsReceipt along with a collection of GoodsReceiptProduct. So what are the possible ways to get that unique object from the list which may get added or updated or deleted on comparing that list with an existing DE list on the server.

Baskaran S
  • 25
  • 10

2 Answers2

1

I guess you want to compare 2 lists and Identify Which records are newly added, or deleted, or remaining as it is. Instead of finding a unique object.

For this, you can use Except & Intersect

For Example:

List<GoodsReceiptProduct> existingListInDataEntity = GetExistingList();
List<GoodsReceiptProduct> modifiedListInBusinessEntity = GetBusinessList();

var newAddedItems = modifiedListInBusinessEntity.Except(existingListInDataEntity);
var deletedItems = existingListInDataEntity.Except(modifiedListInBusinessEntity);
var sameItems = modifiedListInBusinessEntity.Intersect(existingListInDataEntity);
Nilay Mehta
  • 1,732
  • 2
  • 20
  • 26
  • @Nilay.This is want exactly i want. but i am getting this error => 'List' does not contain a definition for 'Except' and the best extension method overload 'ParallelEnumerable.Except(ParallelQuery, IEnumerable)' requires a receiver of type 'ParallelQuery' – Baskaran S Mar 13 '19 at 06:53
  • Got the error because of MisMatch between DE and BE. Thanks a lot for your information. – Baskaran S Mar 13 '19 at 07:08
  • You need to include `System.Linq` namespace. If both models are a different kind then you can obtain `List` first and then compare that Guids – Nilay Mehta Mar 13 '19 at 08:34
-1

Use Distinct() in IEnumerables

Eg:

var YourList = List<GoodsReceiptProduct>();

//YourList = propulate the list here;

var UniqueList = YourList.Distinct();
KrishnaDhungana
  • 2,604
  • 4
  • 25
  • 37
  • What is more than one item added? Does Distinct return those newly added items? – Baskaran S Mar 13 '19 at 06:20
  • Distinct always returns all items that are distinct that. You can also use the package `MoreLinq` to use `DistinctBy` to decide what key should be used to decide if something is distinct or not – sk2andy Mar 13 '19 at 06:40
  • @BaskaranS Distinct will return "Distinct" List Items of your list (Whatever items exists at the time of executing the Distinct Method). – Vidura Silva Mar 19 '19 at 05:51