代码之家  ›  专栏  ›  技术社区  ›  GWLlosa

LINQ选择与匿名类型不同

  •  161
  • GWLlosa  · 技术社区  · 17 年前

    myObjectCollection.Select(item=>new
                                    {
                                         Alpha = item.propOne,
                                         Bravo = item.propTwo
                                    }
                     ).Distinct();
    

    8 回复  |  直到 17 年前
        1
  •  206
  •   Matt Hamilton    17 年前

    And Equality for All ... Anonymous Types

    类型。二者的实施 重写方法使用所有公共 计算类型的属性 对象的哈希码和测试 平等。如果两个对象相同 匿名类型都是一样的 其属性值

    因此,在返回匿名类型的查询上使用Distinct()方法是完全安全的。

        2
  •  14
  •   Nabin Nepal Nabin Nepal    17 年前
    public class DelegateComparer<T> : IEqualityComparer<T>
    {
        private Func<T, T, bool> _equals;
        private Func<T, int> _hashCode;
        public DelegateComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
        {
            _equals= equals;
            _hashCode = hashCode;
        }
        public bool Equals(T x, T y)
        {
            return _equals(x, y);
        }
    
        public int GetHashCode(T obj)
        {
            if(_hashCode!=null)
                return _hashCode(obj);
            return obj.GetHashCode();
        }       
    }
    
    public static class Extensions
    {
        public static IEnumerable<T> Distinct<T>(this IEnumerable<T> items, 
            Func<T, T, bool> equals, Func<T,int> hashCode)
        {
            return items.Distinct(new DelegateComparer<T>(equals, hashCode));    
        }
        public static IEnumerable<T> Distinct<T>(this IEnumerable<T> items,
            Func<T, T, bool> equals)
        {
            return items.Distinct(new DelegateComparer<T>(equals,null));
        }
    }
    
    var uniqueItems=students.Select(s=> new {FirstName=s.FirstName, LastName=s.LastName})
                .Distinct((a,b) => a.FirstName==b.FirstName, c => c.FirstName.GetHashCode()).ToList();
    

    很抱歉之前的格式混乱

        3
  •  5
  •   GeorgeBarker    17 年前

    有趣的是,它在C#中工作,但在VB中不工作

    var MyBet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
    MyBet.ToCharArray()
    .Select(x => new {lower = x.ToString().ToLower(), upper = x.ToString().ToUpper()})
    .Distinct()
    .Dump();
    

    返回52。。。

    Dim MyBet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"
    MyBet.ToCharArray() _
    .Select(Function(x) New With {.lower = x.ToString.ToLower(), .upper = x.ToString.ToUpper()}) _
    .Distinct() _
    .Dump()
    
        4
  •  4
  •   tvanfosson    17 年前

        5
  •  2
  •   Buildstarted    15 年前

    创建一个从IEqualityComparer接口派生的类

    public class DelegateComparer<T> : IEqualityComparer<T>
    {
        private Func<T, T, bool> _equals;
        private Func<T, int> _hashCode;
        public DelegateComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
        {
            _equals= equals;
            _hashCode = hashCode;
        }
        public bool Equals(T x, T y)
        {
            return _equals(x, y);
        }
    
        public int GetHashCode(T obj)
        {
            if(_hashCode!=null)
                return _hashCode(obj);
            return obj.GetHashCode();
        }       
    }
    

    然后创建您的Distinct Extension方法

    public static class Extensions
    {
        public static IEnumerable<T> Distinct<T>(this IEnumerable<T> items, 
            Func<T, T, bool> equals, Func<T,int> hashCode)
        {
            return items.Distinct(new DelegateComparer<T>(equals, hashCode));    
        }
        public static IEnumerable<T> Distinct<T>(this IEnumerable<T> items,
            Func<T, T, bool> equals)
        {
            return items.Distinct(new DelegateComparer<T>(equals,null));
        }
    }
    

    您可以使用此方法查找不同的项目

    var uniqueItems=students.Select(s=> new {FirstName=s.FirstName, LastName=s.LastName})
                .Distinct((a,b) => a.FirstName==b.FirstName, c => c.FirstName.GetHashCode()).ToList();
    
        6
  •  0
  •   ern    17 年前

    如果 Alpha 和 Bravo IEquatable<T> .

    例如:

    public class CommonClass : IEquatable<CommonClass>
    {
        // needed for Distinct()
        public override int GetHashCode() 
        {
            return base.GetHashCode();
        }
    
        public bool Equals(CommonClass other)
        {
            if (other == null) return false;
            return [equality test];
        }
    }
    
        7
  •  0
  •   Modather Sadik    15 年前

    嘿,我也遇到了同样的问题,我找到了解决方案。 你必须实现IEquatable接口,或者简单地重写 (Equals&GetHashCode)方法。但这不是技巧,而是GetHashCode方法中的技巧。您不应该返回类对象的哈希码,但应该像这样返回要比较的属性的哈希。

    public override bool Equals(object obj)
        {
            Person p = obj as Person;
            if ( obj == null )
                return false;
            if ( object.ReferenceEquals( p , this ) )
                return true;
            if ( p.Age == this.Age && p.Name == this.Name && p.IsEgyptian == this.IsEgyptian )
                return true;
            return false;
            //return base.Equals( obj );
        }
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    

    如您所见,我有一个名为person的类,它有3个属性(Name、Age、IsEgyptian“因为我是”)。在GetHashCode中,我返回了Name属性的哈希值,而不是person对象的哈希值。

    试试看,ISA会奏效的。

        8
  •  0
  •   Alisson Reinaldo Silva    8 年前

    为了使其在VB.NET中工作,您需要指定 Key

    myObjectCollection.Select(Function(item) New With
    {
        Key .Alpha = item.propOne,
        Key .Bravo = item.propTwo
    }).Distinct()