using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public Int32 FormID { get; set; }
public String ProductLineDesc { get; set; }
}
public class Products : List<Product>
{
public Products()
{
base.Add(new Product { FormID = 1, ProductLineDesc = "abc" });
base.Add(new Product { FormID = 2, ProductLineDesc = "abc" });
base.Add(new Product { FormID = 1, ProductLineDesc = "xyz" });
base.Add(new Product { FormID = 2, ProductLineDesc = "def" });
base.Add(new Product { FormID = 3, ProductLineDesc = "abc" });
base.Add(new Product { FormID = 3, ProductLineDesc = "xyz" });
}
}
public class UniqueProductLineDesc : IEqualityComparer<Product>
{
public Boolean Equals(Product a, Product b)
{
if (Object.ReferenceEquals(a, b))
return true;
if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null))
return false;
return String.Compare(a.ProductLineDesc, b.ProductLineDesc, true) == 0;
}
public Int32 GetHashCode(Product product)
{
if (Object.ReferenceEquals(product, null))
return 0;
return product.ProductLineDesc == null ? 0 : product.ProductLineDesc.GetHashCode();
}
}
public class Test
{
public static void Main()
{
Products DB_Products = new Products();
IEnumerable<Product> products = DB_Products.OrderBy(p => p.FormID).Distinct(new UniqueProductLineDesc()).ToList();
foreach (Product product in products)
{
Console.WriteLine("{0}. {1}", product.FormID, product.ProductLineDesc);
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnM7CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwp1c2luZyBTeXN0ZW0uTGlucTsKCnB1YmxpYyBjbGFzcyBQcm9kdWN0CnsKCXB1YmxpYyBJbnQzMiBGb3JtSUQgeyBnZXQ7IHNldDsgfQoJcHVibGljIFN0cmluZyBQcm9kdWN0TGluZURlc2MgeyBnZXQ7IHNldDsgfQp9CgpwdWJsaWMgY2xhc3MgUHJvZHVjdHMgOiBMaXN0PFByb2R1Y3Q+CnsKCXB1YmxpYyBQcm9kdWN0cygpCgl7CgkJYmFzZS5BZGQobmV3IFByb2R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