class obj
{
int typeId; //10 types 0-9
string uniqueString; //this is unique
}
Assume there is list with 100 elements of obj, but only 10
If just want to use Linq, you can override Equals and GetHashCode methods.
Product class:
public class Product
{
public string ProductName { get; set; }
public int Id { get; set; }
public override bool Equals(object obj)
{
if (!(obj is Product))
{
return false;
}
var other = (Product)obj;
return Id == other.Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
Main Method:
static void Main(string[] args)
{
var products = new List
{
new Product{ ProductName="Product 1",Id = 1},
new Product{ ProductName="Product 2",Id = 2},
new Product{ ProductName="Product 4",Id = 5},
new Product{ ProductName="Product 3",Id = 3},
new Product{ ProductName="Product 4",Id = 4},
new Product{ ProductName="Product 6",Id = 4},
new Product{ ProductName="Product 6",Id = 4},
};
var itemsDistinctByProductName = products.Distinct().ToList();
foreach (var product in itemsDistinctByProductName)
{
Console.WriteLine($"Product Id : {product.Id} ProductName : {product.ProductName} ");
}
Console.ReadKey();
}