I want to remove the duplicates from the List, for that I am using following code:
public List
public class ContainerLevelDetailsResponse
{
public long ContainerId { get; set; }
public string Container { get; set; }
public string Pallet { get; set; }
public string SKU { get; set; }
public long CaseCount { get; set; }
}
List
new List
booking.ContainerLevelDetails = containerLevelDetails.Distinct().ToList
But its not working and did not giving unique records. Its not able to remove the duplicates.
Please suggest in this.
Sachin SinghPosted Jul 23, 2021, 4:04 PM
kaushik debnathPosted Jul 23, 2021, 3:09 PM
Any of the three solutions did not worked for me.
ContainerLevelDetails { get; set; }
public List
public class ContainerLevelDetailsResponse
{
public long ContainerId { get; set; }
public string Container { get; set; }
public string Pallet { get; set; }
public string SKU { get; set; }
public long CaseCount { get; set; }
}
public class DistinctItemComparer : IEqualityComparer
(); containerLevelDetails = new List();
{
public bool Equals(ContainerLevelDetailsResponse x, ContainerLevelDetailsResponse y)
{
return x.ContainerId == y.ContainerId &&
x.Container == y.Container &&
x.Pallet == y.Pallet &&
x.SKU == y.SKU &&
x.CaseCount == y.CaseCount;
}
public int GetHashCode(ContainerLevelDetailsResponse obj)
{
return obj.ContainerId.GetHashCode() ^
obj.Container.GetHashCode() ^
obj.Pallet.GetHashCode() ^
obj.SKU.GetHashCode() ^
obj.CaseCount.GetHashCode();
}
}
var dtShipmentDetails = dtTrace.Tables[4];
if(dtShipmentDetails.Rows.Count > 0)
{
traceDetails.Shipments = new List
List
foreach (DataRow rd in dtShipmentDetails.Rows)
{
var bookingRes = JsonConvert.DeserializeObject
>((string)rd["BookingData"]);
var booking = bookingRes[0];
booking.BookingId = (long)rd["BookingId"];
booking.BookingTypeId = (long)rd["BookingTypeId"];
booking.BookingType = (string)rd["BookingType"];
booking.ContainerCount = (int)rd["ContainerCount"];
#region User Story 4107 - Implementation
containerLevelDetails = JsonConvert.DeserializeObject
> ((string)rd["ContainerLevelDetails"]);
booking.ContainerLevelDetails = containerLevelDetails.Distinct(new DistinctItemComparer()).ToList();
traceDetails.Shipments.Add(booking);
}
public class DistinctItemComparer : IEqualityComparer
{
public bool Equals(ContainerLevelDetailsResponse x, ContainerLevelDetailsResponse y)
{
return x.ContainerId == y.ContainerId &&
x.Container == y.Container &&
x.Pallet == y.Pallet &&
x.SKU == y.SKU &&
x.CaseCount == y.CaseCount;
}
public int GetHashCode(ContainerLevelDetailsResponse obj)
{
return obj.ContainerId.GetHashCode() ^
obj.Container.GetHashCode() ^
obj.Pallet.GetHashCode() ^
obj.SKU.GetHashCode() ^
obj.CaseCount.GetHashCode();
}
}
Duplicates records still coming
Sachin SinghPosted Jul 21, 2021, 8:12 PM