Distinct() with lambda?

前端 未结 18 960
南旧
南旧 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:25

    If Distinct() doesn't produce unique results, try this one:

    var filteredWC = tblWorkCenter.GroupBy(cc => cc.WCID_I).Select(grp => grp.First()).Select(cc => new Model.WorkCenter { WCID = cc.WCID_I }).OrderBy(cc => cc.WCID); 
    
    ObservableCollection<Model.WorkCenter> WorkCenter = new ObservableCollection<Model.WorkCenter>(filteredWC);
    
    0 讨论(0)
  • 2020-11-22 06:26
    IEnumerable<Customer> filteredList = originalList
      .GroupBy(customer => customer.CustomerId)
      .Select(group => group.First());
    
    0 讨论(0)
  • 2020-11-22 06:26

    To Wrap things up . I think most of the people which came here like me want the simplest solution possible without using any libraries and with best possible performance.

    (The accepted group by method for me i think is an overkill in terms of performance. )

    Here is a simple extension method using the IEqualityComparer interface which works also for null values.

    Usage:

    var filtered = taskList.DistinctBy(t => t.TaskExternalId).ToArray();
    

    Extension Method Code

    public static class LinqExtensions
    {
        public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
        {
            GeneralPropertyComparer<T, TKey> comparer = new GeneralPropertyComparer<T,TKey>(property);
            return items.Distinct(comparer);
        }   
    }
    public class GeneralPropertyComparer<T,TKey> : IEqualityComparer<T>
    {
        private Func<T, TKey> expr { get; set; }
        public GeneralPropertyComparer (Func<T, TKey> expr)
        {
            this.expr = expr;
        }
        public bool Equals(T left, T right)
        {
            var leftProp = expr.Invoke(left);
            var rightProp = expr.Invoke(right);
            if (leftProp == null && rightProp == null)
                return true;
            else if (leftProp == null ^ rightProp == null)
                return false;
            else
                return leftProp.Equals(rightProp);
        }
        public int GetHashCode(T obj)
        {
            var prop = expr.Invoke(obj);
            return (prop==null)? 0:prop.GetHashCode();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:28

    The Microsoft System.Interactive package has a version of Distinct that takes a key selector lambda. This is effectively the same as Jon Skeet's solution, but it may be helpful for people to know, and to check out the rest of the library.

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

    Take another way:

    var distinctValues = myCustomerList.
    Select(x => x._myCaustomerProperty).Distinct();
    

    The sequence return distinct elements compare them by property '_myCaustomerProperty' .

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

    You can use InlineComparer

    public class InlineComparer<T> : IEqualityComparer<T>
    {
        //private readonly Func<T, T, bool> equalsMethod;
        //private readonly Func<T, int> getHashCodeMethod;
        public Func<T, T, bool> EqualsMethod { get; private set; }
        public Func<T, int> GetHashCodeMethod { get; private set; }
    
        public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
        {
            if (equals == null) throw new ArgumentNullException("equals", "Equals parameter is required for all InlineComparer instances");
            EqualsMethod = equals;
            GetHashCodeMethod = hashCode;
        }
    
        public bool Equals(T x, T y)
        {
            return EqualsMethod(x, y);
        }
    
        public int GetHashCode(T obj)
        {
            if (GetHashCodeMethod == null) return obj.GetHashCode();
            return GetHashCodeMethod(obj);
        }
    }
    

    Usage sample:

      var comparer = new InlineComparer<DetalleLog>((i1, i2) => i1.PeticionEV == i2.PeticionEV && i1.Etiqueta == i2.Etiqueta, i => i.PeticionEV.GetHashCode() + i.Etiqueta.GetHashCode());
      var peticionesEV = listaLogs.Distinct(comparer).ToList();
      Assert.IsNotNull(peticionesEV);
      Assert.AreNotEqual(0, peticionesEV.Count);
    

    Source: https://stackoverflow.com/a/5969691/206730
    Using IEqualityComparer for Union
    Can I specify my explicit type comparator inline?

    0 讨论(0)
提交回复
热议问题