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