Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use this nested lambda expression?

Tags:

c#

lambda

I'm trying to maintain a list of unique models from a variety of queries. Unfortunately, the equals method of our models are not defined, so I couldn't use a hash map easily.

As a quick fix I used the following code:

public void AddUnique(
    List<Model> source,
    List<Model> result)
{
    if (result != null)
    {
        if (result.Count > 0
            && source != null
            && source.Count > 0)
        {
            source.RemoveAll(
                s => result.Contains(
                    r => r.ID == s.ID));
        }

        result.AddRange(source);
    }
}

Unfortunately, this does not work. When I step throught the code, I find that even though I've checked to make sure that there was at least one Model with the same ID in both source and result, the RemoveAll(Predicate<Model>) line does not change the number of items in source. What am I missing?

like image 326
Kyle Sletten Avatar asked Mar 24 '26 10:03

Kyle Sletten


2 Answers

The above code shouldn't even compile, as Contains expects a Model, not a predicate.

You can use Any() instead:

source.RemoveAll(s => result.Any(r => r.ID == s.ID));

This will remove the items from source correctly.

like image 187
Reed Copsey Avatar answered Mar 27 '26 02:03

Reed Copsey


I might opt to tackle the problem a different way.

You said you do not have suitable implementations of equality inside the class. Maybe you can't change that. However, you can define an IEqualityComparer<Model> implementation that allows you to specify appropriate Equals and GetHashCode implementations external to the actual Model class itself.

var comparer = new ModelComparer();
var addableModels = newSourceOfModels.Except(modelsThatAlreadyExist, comparer);
// you can then add the result to the existing

Where you might define the comparer as

class ModelComparer : IEqualityComparer<Model>
{
     public bool Equals(Model x, Model y)
     {
         // validations omitted
         return x.ID == y.ID;
     }

     public int GetHashCode(Model m)
     {
         return m.ID.GetHashCode();
     }
}
like image 29
Anthony Pegram Avatar answered Mar 27 '26 01:03

Anthony Pegram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!