简体   繁体   中英

Distinct of items in dictionary<T,IEnumerable<T>>

I am Looking for a cleaner way to perform the following operation:

Filter out distinct values from a dictionary of type Dictionary<T,IEnumerable<T> > , based on the uniqueness of the Value (ie unique by one of the attributes of T in the IEnumerable<T> ).

We can ignore the key on the dictionary. Can someone suggest a good way of achieving the above ?

Jurgen Camilleri has the piece for 'SelectMany().Distinct()'. For the comparer, we use the following comparer all the time to compare based on a property:

Usage

//this uses 'Product' for 'T'.  If you want to just use 'T' you'd have to constrain it

var distinctValues = dictionary.SelectMany((entry) => entry.Value)
.Distinct(new LambdaEqualityComparer<Product>(p=>p.ProductName));

Code

public class LambdaEqualityComparer<T> : IEqualityComparer<T>
    {
        private Func<T,object> _action;
        public LambdaEqualityComparer(Func<T, object> action)
        {
            _action = action;
        }

        public bool Equals(T x, T y)
        {
            var areEqual = baseCheck(x, y) ?? baseCheck(getObj(x), getObj(y));

            if (areEqual != null)
            {
                return areEqual.Value;
            }

            return _action(x).Equals(_action(y));
        }

        public int GetHashCode(T obj)
        {
            return _action(obj).GetHashCode();
        }

        /// <summary>
        /// True = return true
        /// False = return false
        /// null = continue evaluating
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private bool? baseCheck(object x, object y)
        {
            if (x == null && y == null)
            {
                return true;
            }
            else if (x == null || y == null)
            {
                return false;
            }

            return null;

        }

        private object getObj(T t)
        {
            try
            {
                return _action(t);
            }
            catch (NullReferenceException)
            {

            }

            return null;
        }
    }

This should work for your case:

IEnumerable<T> allValues = dictionary.SelectMany((entry) => entry.Value);
IEnumerable<T> distinctValues = allValues.Distinct(comparer);

Make sure that you create a comparer class which implements IEqualityComparer<T> so that Distinct() can differentiate between instances of T (unless T is a class which already has a comparer in the .NET Framework such as System.String ).

I wrote a method that will merge dictionaries removing duplicates. Perhaps you could use the guts of this to perform something similar.

public static Dictionary<TKey, List<TValue>> MergeDictionaries<TKey, TValue>(this IEnumerable<Dictionary<TKey, List<TValue>>> dictionaries)
        {
            return dictionaries.SelectMany(dict => dict)
                         .ToLookup(pair => pair.Key, pair => pair.Value)
                         .ToDictionary(group => group.Key, group => group.SelectMany(value => value).Distinct().ToList());
        }

Maybe, just this section:

.ToLookup(pair => pair.Key, pair => pair.Value)
.ToDictionary(group => group.Key, group => group.SelectMany(value => value).Distinct().ToList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM