Reputation: 63
Here is the sample data:
public static List<DemoDataBindingByAnalysis> DataBindingByAnalysis
{
get
{
return new List<DemoDataBindingByAnalysis> {
new DemoDataBindingByAnalysis {
BCCode = "9105101",
BCName = "Allie Enterprise",
ItemCode = "1137806",
ItemDesc = "My Lunch Box Set (4)",
StandardPack = 8,
Amount = 46,
Date = DateTime.Parse("2018/03/05")
},
new DemoDataBindingByAnalysis {
BCCode = "9105101",
BCName = "Allie Enterprise",
ItemCode = "1137806",
ItemDesc = "My Lunch Box Set (4)",
StandardPack = 8,
Amount = 6,
Date = DateTime.Parse("2018/05/12")
},....
I've already tried using Distinct
and GroupBy
but the result is not what I expected. The result I want is like :
bccode1 | bcname | itemcode | item Desc | Stand Pack | Amount1 | Date1 |Amount2 | Date2
Upvotes: 0
Views: 81
Reputation: 6522
You can simply do this as per JohanP's comment;
MoreLinq DistinctBy:
var distinctData = DataBindingByAnalysis.DistinctBy(m => new { m.BCCode, m.BCName, ItemCode }).ToList();
Add more fields as per your requirements.
MoreLinq is here
else need to implement IEqualityComparer
and then can use Distinct
:
Sample code: (this is a reference, please modify as per your code)
class DemoDataEqualityComparer : IEqualityComparer<DemoDataBindingByAnalysis>
{
public bool Equals(DemoDataBindingByAnalysis x, DemoDataBindingByAnalysis y)
{
return x.BCCode.Equals(y.BCCCode) && y.BCName.Equals(y.BCName); //&& more fields here.
}
public int GetHashCode(DemoDataBindingByAnalysis obj)
{
return obj.BCCode.GetHashCode();
}
}
Upvotes: 1