Get distinct list values

后端 未结 8 819
余生分开走
余生分开走 2020-12-17 17:58

i have a C# application in which i\'d like to get from a List of Project objects , another List which contains distinct objects.

i trie

相关标签:
8条回答
  • 2020-12-17 18:24

    Check this example: you need to use either Comparator or override Equals()

    class Program
    {
        static void Main( string[] args )
        {
            List<Item> items = new List<Item>();
            items.Add( new Item( "A" ) );
            items.Add( new Item( "A" ) );
            items.Add( new Item( "B" ) );
            items.Add( new Item( "C" ) );
    
            items = items.Distinct().ToList();
        }
    }
    
    public class Item
    {
        string Name { get; set; }
        public Item( string name )
        {
            Name = name;
        }
    
        public override bool Equals( object obj )
        {
            return Name.Equals((obj as Item).Name);
        }
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:30

    You need to define "identical" here. I'm guessing you mean "have the same contents", but that is not the default definition for classes: the default definition is "are the same instance".

    If you want "identical" to mean "have the same contents", you have two options:

    • write a custom comparer (IEqualityComparer<Project>) and supply that as a parameter to Distinct
    • override Equals and GetHashCode on Project

    There are also custom methods like DistinctBy that are available lots of places, which is useful if identity can be determined by a single property (Id, typically) - not in the BCL, though. But for example:

    if (model != null) model = model.DistinctBy(x => x.Id).ToList();
    

    With, for example:

    public static IEnumerable<TItem>
        DistinctBy<TItem, TValue>(this IEnumerable<TItem> items,
        Func<TItem, TValue> selector)
    {
        var uniques = new HashSet<TValue>();
        foreach(var item in items)
        {
            if(uniques.Add(selector(item))) yield return item;
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:34

    Here's an answer from basically the same question that will help.

    Explanation:

    The Distinct() method checks reference equality for reference types. This means it is looking for literally the same object duplicated, not different objects which contain the same values.

    Credits to @Rex M.

    0 讨论(0)
  • 2020-12-17 18:35

    The object's reference aren't equal. If you want to be able to do that on the entire object itself and not just a property, you have to implement the IEqualityComparer or IEquatable<T>.

    0 讨论(0)
  • 2020-12-17 18:38
    var newList = 
    (
    from x in model
    select new {Id_user= x.Id_user}
    ).Distinct();
    

    or you can write like this

    var list1 = model.DistinctBy(x=> x.Id_user);
    
    0 讨论(0)
  • 2020-12-17 18:39

    How do you define identical? You should override Equals in Project with this definition (if you override Equals also override GetHashCode). For example:

    public class Project
    {
        public int ProjectID { get; set; }
    
        public override bool Equals(object obj)
        {
            var p2 = obj as Project;
            if (p2 == null) return false;
            return this.ProjectID == m2.ProjectID;
        }
    
        public override int GetHashCode()
        {
            return ProjectID;
        }
    }
    

    Otherwise you are just checking reference equality.

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