SoFunction
Updated on 2025-03-07

C# EF removes duplicate column DistinctBy method

C# EF removes duplicate column DistinctBy

I saw LinQ has a DistinctBy method online, but I didn't find it when I actually used it. Later, I referenced the website and found out that it was written as an extension method.

https:///article/

1. Add an extension method

    public static class DistinctByClass
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if ((keySelector(element)))
                {
                    yield return element;
                }
            }
        }
    }

2. The usage method is as follows (Distinct for ID and Name)

var query = (p => new { ,  });

3. If you only distinguish it for ID:

var query = (p => );

Use Distinct to remove duplicate elements in C# collection, IEqualityComparer<T> principle

json test data

[
    {
        "Type": "ROLETYPE_USER",
        "Value": "9846cdac-ae21-4be4-a284-50158dd19606"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "4f6665fc-b0b9-4865-bf12-eff06810efa3"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "7c4852c2-b2c2-4688-92a3-3dd1b00bacf8"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "982a2f9d-a079-4613-825f-c2ef9801eb3e"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
    },
    {
        "Type": "ROLETYPE_ROLE",
        "Value": "76f62bf8-bf24-48c1-b70e-7628ff08c3fb"
    },
    {
        "Type": "ROLETYPE_USER",
        "Value": "982a2f9d-a079-4613-825f-c2ef9801eb3e"
    }
]

Entity Type

    public class TempSetting
    {
       public string Type { get; set; }
        public Guid Value { get; set; }
    }

Create a comparison class, inherited from IEqualityComparer<TSource>. Generally, you need to select the eigenvalue field to set the hashCode value to return to the effect, or directly set the hash value to return 1 (this will call the Equals method every time, but the performance will be low).

IEqualityComparer compares whether the objects are equal. It is preferred to call GetHashCode() to compare whether the hash values ​​are equal. If the hash values ​​are not equal, the Equals method will not be called. If the hash values ​​are equal, the Equals method will be called and then the final confirmation will be confirmed. Therefore, the GetHashCode method must set the eigenvalue field to return hashcode, so that the speed is faster and the performance is high.

 public class CompareSetting : IEqualityComparer&lt;TempSetting&gt;
    {
        public bool Equals(TempSetting x, TempSetting y)
        {
            return  == ;
        }
 
        public int GetHashCode(TempSetting obj)
        {
            if (obj == null)
            {
                return 0;
            }
            //return ();//return ();            //string km = + ;//Compare the repeated values ​​successfully            //return ();
            //GetHashCode recommends selecting the feature value field you like to compare, otherwise sometimes the comparison may not be successful.            return ();
        }
    }
 
//Test comparison        string funs = "[{\"Type\":\"ROLETYPE_USER\",\"Value\":\"9846cdac-ae21-4be4-a284-50158dd19606\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"4f6665fc-b0b9-4865-bf12-eff06810efa3\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"7c4852c2-b2c2-4688-92a3-3dd1b00bacf8\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"982a2f9d-a079-4613-825f-c2ef9801eb3e\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_ROLE\",\"Value\":\"76f62bf8-bf24-48c1-b70e-7628ff08c3fb\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"982a2f9d-a079-4613-825f-c2ef9801eb3e\"}]";
            List&lt;TempSetting&gt; list = &lt;List&lt;TempSetting&gt;&gt;(funs);
            var list2 = (new CompareSetting());

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.