Get distinct list values

后端 未结 8 820
余生分开走
余生分开走 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:41

    Isn't simpler to use one of the approaches shown below :) ? You can just group your domain objects by some key and select FirstOrDefault like below.

    More interesting option is to create some Comparer adapter that takes you domain object and creates other object the Comparer can use/work with out of the box. Base on the comparer you can create your custom linq extensions like in sample below. Hope it helps :)

    [TestMethod]
        public void CustomDistinctTest()
        {
            // Generate some sample of domain objects
            var listOfDomainObjects = Enumerable
                                        .Range(10, 10)
                                        .SelectMany(x => 
                                            Enumerable
                                            .Range(15, 10)
                                            .Select(y => new SomeClass { SomeText = x.ToString(), SomeInt = x + y }))
                                        .ToList();
    
            var uniqueStringsByUsingGroupBy = listOfDomainObjects
                                            .GroupBy(x => x.SomeText)
                                            .Select(x => x.FirstOrDefault())
                                            .ToList();
    
            var uniqueStringsByCustomExtension = listOfDomainObjects.DistinctBy(x => x.SomeText).ToList();
            var uniqueIntsByCustomExtension = listOfDomainObjects.DistinctBy(x => x.SomeInt).ToList();
    
            var uniqueStrings = listOfDomainObjects
                                    .Distinct(new EqualityComparerAdapter<SomeClass, string>(x => x.SomeText))
                                    .OrderBy(x=>x.SomeText)
                                    .ToList();
    
            var uniqueInts = listOfDomainObjects
                                    .Distinct(new EqualityComparerAdapter<SomeClass, int>(x => x.SomeInt))
                                    .OrderBy(x => x.SomeInt)
                                    .ToList();
        }
    

    Custom comparer adapter:

    public class EqualityComparerAdapter<T, V> : EqualityComparer<T>
        where V : IEquatable<V>
    {
        private Func<T, V> _valueAdapter;
    
        public EqualityComparerAdapter(Func<T, V> valueAdapter)
        {
            _valueAdapter = valueAdapter;
        }
    
        public override bool Equals(T x, T y)
        {
            return _valueAdapter(x).Equals(_valueAdapter(y));
        }
    
        public override int GetHashCode(T obj)
        {
            return _valueAdapter(obj).GetHashCode();
        }
    }
    

    Custom linq extension (definition of DistinctBy extension method):

    // Embedd this class in some specific custom namespace
    public static class DistByExt
    {
        public static IEnumerable<T> DistinctBy<T,V>(this IEnumerable<T> enumerator,Func<T,V> valueAdapter)
            where V : IEquatable<V>
        {
            return enumerator.Distinct(new EqualityComparerAdapter<T, V>(valueAdapter));
        }
    }
    

    Definition of domain object used in test case:

    public class SomeClass
    {
        public string SomeText { get; set; }
        public int SomeInt { get; set; }
    
    }
    
    0 讨论(0)
  • 2020-12-17 18:41
    List<ViewClReceive> passData = (List<ViewClReceive>)TempData["passData_Select_BankName_List"];
        passData = passData?.DistinctBy(b=>b.BankNm).ToList();
    

    It will Works ......

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