Distinct() with lambda?

前端 未结 18 952
南旧
南旧 2020-11-22 06:04

Right, so I have an enumerable and wish to get distinct values from it.

Using System.Linq, there\'s of course an extension method called Distinct<

相关标签:
18条回答
  • 2020-11-22 06:39

    All solutions I've seen here rely on selecting an already comparable field. If one needs to compare in a different way, though, this solution here seems to work generally, for something like:

    somedoubles.Distinct(new LambdaComparer<double>((x, y) => Math.Abs(x - y) < double.Epsilon)).Count()
    
    0 讨论(0)
  • 2020-11-22 06:44

    I'm assuming you have an IEnumerable, and in your example delegate, you would like c1 and c2 to be referring to two elements in this list?

    I believe you could achieve this with a self join var distinctResults = from c1 in myList join c2 in myList on

    0 讨论(0)
  • 2020-11-22 06:46

    It looks to me like you want DistinctBy from MoreLINQ. You can then write:

    var distinctValues = myCustomerList.DistinctBy(c => c.CustomerId);
    

    Here's a cut-down version of DistinctBy (no nullity checking and no option to specify your own key comparer):

    public static IEnumerable<TSource> DistinctBy<TSource, TKey>
         (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> knownKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (knownKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:46

    You can use LambdaEqualityComparer:

    var distinctValues
        = myCustomerList.Distinct(new LambdaEqualityComparer<OurType>((c1, c2) => c1.CustomerId == c2.CustomerId));
    
    
    public class LambdaEqualityComparer<T> : IEqualityComparer<T>
        {
            public LambdaEqualityComparer(Func<T, T, bool> equalsFunction)
            {
                _equalsFunction = equalsFunction;
            }
    
            public bool Equals(T x, T y)
            {
                return _equalsFunction(x, y);
            }
    
            public int GetHashCode(T obj)
            {
                return obj.GetHashCode();
            }
    
            private readonly Func<T, T, bool> _equalsFunction;
        }
    
    0 讨论(0)
  • 2020-11-22 06:46

    A tricky way to do this is use Aggregate() extension, using a dictionary as accumulator with the key-property values as keys:

    var customers = new List<Customer>();
    
    var distincts = customers.Aggregate(new Dictionary<int, Customer>(), 
                                        (d, e) => { d[e.CustomerId] = e; return d; },
                                        d => d.Values);
    

    And a GroupBy-style solution is using ToLookup():

    var distincts = customers.ToLookup(c => c.CustomerId).Select(g => g.First());
    
    0 讨论(0)
  • 2020-11-22 06:47

    Here's how you can do it:

    public static class Extensions
    {
        public static IEnumerable<T> MyDistinct<T, V>(this IEnumerable<T> query,
                                                        Func<T, V> f, 
                                                        Func<IGrouping<V,T>,T> h=null)
        {
            if (h==null) h=(x => x.First());
            return query.GroupBy(f).Select(h);
        }
    }
    

    This method allows you to use it by specifying one parameter like .MyDistinct(d => d.Name), but it also allows you to specify a having condition as a second parameter like so:

    var myQuery = (from x in _myObject select x).MyDistinct(d => d.Name,
            x => x.FirstOrDefault(y=>y.Name.Contains("1") || y.Name.Contains("2"))
            );
    

    N.B. This would also allow you to specify other functions like for example .LastOrDefault(...) as well.


    If you want to expose just the condition, you can have it even simpler by implementing it as:

    public static IEnumerable<T> MyDistinct2<T, V>(this IEnumerable<T> query,
                                                    Func<T, V> f,
                                                    Func<T,bool> h=null
                                                    )
    {
        if (h == null) h = (y => true);
        return query.GroupBy(f).Select(x=>x.FirstOrDefault(h));
    }
    

    In this case, the query would just look like:

    var myQuery2 = (from x in _myObject select x).MyDistinct2(d => d.Name,
                        y => y.Name.Contains("1") || y.Name.Contains("2")
                        );
    

    N.B. Here, the expression is simpler, but note .MyDistinct2 uses .FirstOrDefault(...) implicitly.


    Note: The examples above are using the following demo class

    class MyObject
    {
        public string Name;
        public string Code;
    }
    
    private MyObject[] _myObject = {
        new MyObject() { Name = "Test1", Code = "T"},
        new MyObject() { Name = "Test2", Code = "Q"},
        new MyObject() { Name = "Test2", Code = "T"},
        new MyObject() { Name = "Test5", Code = "Q"}
    };
    
    0 讨论(0)
提交回复
热议问题