how to compare two entity objects in c 23 to update

Solutions on MaxInterview for how to compare two entity objects in c 23 to update by the best coders in the world

showing results for - "how to compare two entity objects in c 23 to update"
Julissa
13 Jan 2019
1var comparer = new ProductNumberEqualityComparer();
2
3var itemsToDelete = ProductsFromDB.Except(ProductsFromTXT, comparer).ToList();
4foreach (Product item in itemsToDelete)
5{
6   // TODO: Delete the product
7}
8
9var itemsToUpdate = from dbProduct in ProductsFromDB
10                    join txtProduct in ProductsFromTXT
11                    on dbProduct.ProductNumber equals txtProduct.ProductNumber
12                    select new
13                    {
14                       dbProduct,
15                       txtProduct
16                    };
17
18foreach (var item in itemsToUpdate)
19{
20   // Update the product:
21   item.dbProduct.Brand = item.txtProduct.Brand;
22   item.dbProduct.Category = item.txtProduct.Category;
23   item.dbProduct.Price = item.txtProduct.Price;
24
25   // TODO: Update the stock items if required
26}
27
28var itemsToAdd = ProductsFromTXT.Except(ProductsFromDB, comparer).ToList();
29foreach (Product item in itemsToAdd)
30{
31   // TODO: Add the product
32}
33
Miller
26 Jan 2017
1public class ProductNumberEqualityComparer : IEqualityComparer<Product>
2{
3   public int GetHashCode(Product obj)
4   {
5      return (obj == null) ? 0 : obj.ProductNumber.GetHashCode();
6   }
7
8   public bool Equals(Product x, Product y)
9   {
10      if (ReferenceEquals(x, y)) return true;
11      if (x == null || y == null) return false;
12      return x.ProductNumber == y.ProductNumber;
13   }
14}
15