how does except method work in linq

后端 未结 5 2164
梦毁少年i
梦毁少年i 2020-12-05 20:45

I have the classes:

class SomeClass
{
   public string Name{get;set;}
   public int SomeInt{get;set;}
}


class SomeComparison: IEqualityComparer

        
5条回答
  •  独厮守ぢ
    2020-12-05 20:52

    Your guess was close - the Linq to Objects Except extension method uses a HashSet internally for the second sequence passed in - that allows it to look up elements in O(1) while iterating over the first sequence to filter out elements that are contained in the second sequence, hence the overall effort is O(n+m) where n and m are the length of the input sequences - this is the best you can hope to do since you have to look at each element at least once.

    For a review of how this might be implemented I recommend Jon Skeet's EduLinq series, here part of it's implementation of Except and the link to the full chapter:

    private static IEnumerable ExceptImpl(
        IEnumerable first,
        IEnumerable second,
        IEqualityComparer comparer)
    {
        HashSet bannedElements = new HashSet(second, comparer);
        foreach (TSource item in first)
        {
            if (bannedElements.Add(item))
            {
                yield return item;
            }
        }
    }
    

    Your first implementation on the other hand will compare each element in the first list to each element in the second list - it is performing a cross product. This will require nm operations so it will run in O(nm) - when n and m become large this becomes prohibitively slow very fast. (Also this solution is wrong as is since it will create duplicate elements).

提交回复
热议问题