So I have two lists which has similar properties for each item in the list. Each item is of the format -
public class ZoneOccupancy
{
public virtual string ZoneId { get; set; }
public virtual int CurrentOccupancy { get; set; }
}
ZoneId | CurrentOccupancy
One list would be like -
ZoneId | CurrentOccupancy
110 2
111 1
115 3
Another list would be like -
ZoneId | CurrentOccupancy
110 1
111 1
116 3
After merging what I want is something like -
ZoneId | CurrentOccupancy
110 3
111 2
115 3
116 3
So i want the duplicate items from the list to merge into one but in the process, the count to be added.
Edit:
list.Union(ParkingTagTicketQueryResult, ZoneComparer.Instance)
.GroupBy(z => z.ZoneId)
.Select(z => new ZoneOccupancy
{
ZoneId = z.First().ZoneId,
CurrentOccupancy = z.Sum(row => row.CurrentOccupancy)
public class ZoneComparer : IEqualityComparer<ZoneOccupancy>
{
public static readonly ZoneComparer Instance = new ZoneComparer();
// We don't need any more instances
private ZoneComparer() { }
public int GetHashCode(ZoneOccupancy z)
{
return z.ZoneId.GetHashCode();
}
public bool Equals(ZoneOccupancy z1, ZoneOccupancy z2)
{
if (Object.ReferenceEquals(z1, z2))
{
return true;
}
if (Object.ReferenceEquals(z1, null) ||
Object.ReferenceEquals(z2, null))
{
return false;
}
return z1.ZoneId.Equals(z2.ZoneId);
}
}