Answers for "how to compare two entity objects in c# to update"

0

how to compare two entity objects in c# to update

var comparer = new ProductNumberEqualityComparer();

var itemsToDelete = ProductsFromDB.Except(ProductsFromTXT, comparer).ToList();
foreach (Product item in itemsToDelete)
{
   // TODO: Delete the product
}

var itemsToUpdate = from dbProduct in ProductsFromDB
                    join txtProduct in ProductsFromTXT
                    on dbProduct.ProductNumber equals txtProduct.ProductNumber
                    select new
                    {
                       dbProduct,
                       txtProduct
                    };

foreach (var item in itemsToUpdate)
{
   // Update the product:
   item.dbProduct.Brand = item.txtProduct.Brand;
   item.dbProduct.Category = item.txtProduct.Category;
   item.dbProduct.Price = item.txtProduct.Price;

   // TODO: Update the stock items if required
}

var itemsToAdd = ProductsFromTXT.Except(ProductsFromDB, comparer).ToList();
foreach (Product item in itemsToAdd)
{
   // TODO: Add the product
}
Posted by: Guest on January-22-2021
0

how to compare two entity objects in c# to update

public class ProductNumberEqualityComparer : IEqualityComparer<Product>
{
   public int GetHashCode(Product obj)
   {
      return (obj == null) ? 0 : obj.ProductNumber.GetHashCode();
   }

   public bool Equals(Product x, Product y)
   {
      if (ReferenceEquals(x, y)) return true;
      if (x == null || y == null) return false;
      return x.ProductNumber == y.ProductNumber;
   }
}
Posted by: Guest on January-22-2021

Code answers related to "how to compare two entity objects in c# to update"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language