Filter Linq EXCEPT on properties

前端 未结 8 1289
南方客
南方客 2020-12-04 17:45

This may seem silly, but all the examples I\'ve found for using Except in linq use two lists or arrays of only strings or integers and filters them based on the

8条回答
  •  有刺的猬
    2020-12-04 18:10

    This is what LINQ needs

    public static IEnumerable Except(this IEnumerable items, IEnumerable other, Func getKey) 
    {
        return from item in items
                join otherItem in other on getKey(item)
                equals getKey(otherItem) into tempItems
                from temp in tempItems.DefaultIfEmpty()
                where ReferenceEquals(null, temp) || temp.Equals(default(T))
                select item; 
    }
    

提交回复
热议问题