CareTaker22
CareTaker22

Reputation: 1300

Comparing Two Collections data with each other

I have two observable collections. 1. TruckItems 2. TruckItemsComparison. Both are exactly the same.

I load data into the first TruckItems collection from EF6, then 10 seconds later I load data into the second collection TruckItemsComparison. Now the new data that was added in my 2nd collection might have been updated lately from another source and I need to only add the latest data that does not yet exist in my first collection.

I want to check if ANY of the id's from my 2nd collection does not match any of the id's in my first collection and then only add the items that does not match.

CODE:

Here is where I load my data:

private async void LoadTrucks()
{
    using (TruckServiceClient service = new TruckServiceClient())
    {
        var items = await service.GetTrucksAsync();

        if (TruckItems.Count == 0)
        {
            foreach (var item in items)
            {
                TruckItems.Add(new TruckItems
                {
                    TruckId = item.TruckId,
                    TruckQuoteId = item.QuoteId,
                    TruckPhaseId = item.CurrentPhaseId,
                    TruckChassisManufacturer = item.ChassisManufacturer,
                    TruckChassisModel = item.ChassisModel,
                    TruckStatus = item.Status,
                    TruckJobNumber = item.JobNumbers,
                    TruckAddedBy = item.AddedBy,
                    TruckClientName = item.ClientName,
                    TruckClientSurname = item.ClientSurname,
                    TruckClientDetail = item.ClientDetail,
                    TruckCurrentPhase = item.CurrentPhase
                });
            }
        }

        foreach (var item in items)
        {
            TruckItemsComparison.Add(new TruckItems
            {
                TruckId = item.TruckId,
                TruckQuoteId = item.QuoteId,
                TruckPhaseId = item.CurrentPhaseId,
                TruckChassisManufacturer = item.ChassisManufacturer,
                TruckChassisModel = item.ChassisModel,
                TruckStatus = item.Status,
                TruckJobNumber = item.JobNumbers,
                TruckAddedBy = item.AddedBy,
                TruckClientName = item.ClientName,
                TruckClientSurname = item.ClientSurname,
                TruckClientDetail = item.ClientDetail,
                TruckCurrentPhase = item.CurrentPhase
            });
        }
    }
}

And here is where I want to compare my two collections:

public void UpdateTrucks()
{
    LoadTrucks();

    if (TruckItems.Count != 0)
    {
        var truckItemsId = TruckItems.Where(x => x.TruckId != 0).First().TruckId;
        foreach (var item in TruckItemsComparison.Where(x => x.TruckId != truckItemsId))
        {
            TruckItems.Add(item);
        }
    }
}

My problem is that it adds the data from both the two collections together, regardless if the id's correspond or not. Clearly my logic here does not work, so can anyone please show me a way of how I can compare the data and only insert id's that do not yet exist in my TruckItems collection. Thanks and please let me know if you need any more information.

Upvotes: 1

Views: 61

Answers (1)

Bijington
Bijington

Reputation: 3751

You can enumerate through each of the items in your TruckItemsComparison by using Except:

public void UpdateTrucks()
{
    LoadTrucks();

    if (TruckItems.Count != 0)
    {
        foreach (var item in TruckItemsComparison.Except(TruckItems))
        {
            TruckItems.Add(item);
        }
    }
}

If all you want to do is compare the Ids of your TruckItems then you can implement your own IEqualityComparer:

internal class TruckItemsComparer : IEqualityComparer<TruckItems>
{
    #region IEqualityComparer Members

    public bool Equals(TruckItems x, TruckItems y)
    {
        return (((x == null) && (y == null)) ||
            ((x != null) && (y != null) && x.TruckId == y.TruckId));
    }

    public int GetHashCode(TruckItems obj)
    {
        return obj. TruckId.GetHashCode();
    }

    #endregion
}

And then use like so:

foreach (var item in TruckItemsComparison.Except(TruckItems, new TruckItemsComparer()))

Upvotes: 1

Related Questions