Created
May 20, 2014 18:42
-
-
Save smwhit/ce078e4e683246113cb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Client | |
{ | |
public class ProductInfo | |
{ | |
public string ProductId { get; set; } | |
public string ProductName { get; set; } | |
public decimal Price { get; set; } | |
} | |
public class ProductIdPriceEqualityComparer : IEqualityComparer<ProductInfo> | |
{ | |
public bool Equals(ProductInfo x, ProductInfo y) | |
{ | |
if (ReferenceEquals(x, y)) return true; | |
if (ReferenceEquals(x, null)) return false; | |
if (ReferenceEquals(y, null)) return false; | |
if (x.GetType() != y.GetType()) return false; | |
return String.Equals(x.ProductId, y.ProductId) && x.Price == y.Price; | |
} | |
public int GetHashCode(ProductInfo obj) | |
{ | |
unchecked | |
{ | |
return ((obj.ProductId != null ? obj.ProductId.GetHashCode() : 0)*397) ^ obj.Price.GetHashCode(); | |
} | |
} | |
} | |
public class ProductIdProductNameEqualityComparer : IEqualityComparer<ProductInfo> | |
{ | |
public bool Equals(ProductInfo x, ProductInfo y) | |
{ | |
if (ReferenceEquals(x, y)) return true; | |
if (ReferenceEquals(x, null)) return false; | |
if (ReferenceEquals(y, null)) return false; | |
if (x.GetType() != y.GetType()) return false; | |
return string.Equals(x.ProductId, y.ProductId) && string.Equals(x.ProductName, y.ProductName); | |
} | |
public int GetHashCode(ProductInfo obj) | |
{ | |
unchecked | |
{ | |
return ((obj.ProductId != null ? obj.ProductId.GetHashCode() : 0) * 397) ^ ((obj.ProductId != null ? obj.ProductName.GetHashCode() : 0)); | |
} | |
} | |
} | |
public class ProductIdEqualityComparer : IEqualityComparer<ProductInfo> | |
{ | |
public bool Equals(ProductInfo x, ProductInfo y) | |
{ | |
return x.ProductId == y.ProductId; | |
} | |
public int GetHashCode(ProductInfo obj) | |
{ | |
return obj.ProductId.GetHashCode(); | |
} | |
} | |
public class ProductNameChangeInfo | |
{ | |
public string OldProductName { get; set; } | |
public string NewProductName { get; set; } | |
public string ProductId { get; set; } | |
} | |
public class PriceChangeInfo | |
{ | |
public decimal OldPrice { get; set; } | |
public decimal NewPrice { get; set; } | |
public string ProductName { get; set; } | |
public string ProductId { get; set; } | |
public decimal PriceChange { get { return NewPrice - OldPrice; } } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var oldProducts = new List<ProductInfo> | |
{ | |
new ProductInfo { ProductId = "1", ProductName = "Marathon", Price = 0.1m }, | |
new ProductInfo { ProductId = "2", ProductName = "Opal Fruits", Price = 0.15m}, | |
new ProductInfo { ProductId = "3", ProductName = "Walkers Ready Salted", Price = 0.12m}, | |
new ProductInfo { ProductId = "5", ProductName = "New Coca Cola", Price = 0.15m} | |
}; | |
var newProducts = new List<ProductInfo> | |
{ | |
new ProductInfo { ProductId = "1", ProductName = "Snickers", Price = 0.5m }, | |
new ProductInfo { ProductId = "2", ProductName = "Starburst", Price = 0.40m}, | |
new ProductInfo { ProductId = "3", ProductName = "Walkers Ready Salted", Price = 0.40m}, | |
new ProductInfo { ProductId = "4", ProductName = "Sour Skittles", Price = 0.55m} | |
}; | |
var addedProducts = newProducts.Except(oldProducts, new ProductIdEqualityComparer()).ToList(); | |
var droppedProducts = oldProducts.Except(newProducts, new ProductIdEqualityComparer()).ToList(); | |
var productsWithDifferentName = newProducts.Except(oldProducts, new ProductIdProductNameEqualityComparer()).ToList(); | |
var productsNameChanges = productsWithDifferentName.Join(oldProducts, | |
op => op.ProductId, | |
np => np.ProductId, | |
(np, op) => new ProductNameChangeInfo | |
{ | |
NewProductName = np.ProductName, | |
OldProductName = op.ProductName, | |
ProductId = op.ProductId | |
}).ToList(); | |
var productsWithDifferentPrices = newProducts.Except(oldProducts, new ProductIdPriceEqualityComparer()).ToList(); | |
var priceChanges = productsWithDifferentPrices.Join(oldProducts, | |
np => np.ProductId, | |
op => op.ProductId, | |
(np, op) => new PriceChangeInfo | |
{ | |
NewPrice = np.Price, | |
OldPrice = op.Price, | |
ProductId = np.ProductId, | |
ProductName = np.ProductName | |
}).ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment