C#: Getting maximum and minimum values of arbitrary properties of all items in a list

前端 未结 8 1606
温柔的废话
温柔的废话 2021-02-02 13:19

I have a specialized list that holds items of type IThing:

public class ThingList : IList
{...}

public interface IThing
{
    Decimal         


        
8条回答
  •  梦毁少年i
    2021-02-02 13:59

    Yes, you should use a delegate and anonymous methods.

    For an example see here.

    Basically you need to implement something similar to the Find method of Lists.

    Here is a sample implementation

    public class Thing
    {
        public int theInt;
        public char theChar;
        public DateTime theDateTime;
        
        public Thing(int theInt, char theChar, DateTime theDateTime)
        {
            this.theInt = theInt;
            this.theChar = theChar;
            this.theDateTime = theDateTime;
        }
        
        public string Dump()
        {
            return string.Format("I: {0}, S: {1}, D: {2}", 
                theInt, theChar, theDateTime);
        }
    }
    
    public class ThingCollection: List
    {
        public delegate Thing AggregateFunction(Thing Best, 
                            Thing Candidate);
        
        public Thing Aggregate(Thing Seed, AggregateFunction Func)
        {
            Thing res = Seed;
            foreach (Thing t in this) 
            {
                res = Func(res, t);
            }
            return res;
        }
    }
    
    class MainClass
    {
        public static void Main(string[] args)
        {
            Thing a = new Thing(1,'z',DateTime.Now);
            Thing b = new Thing(2,'y',DateTime.Now.AddDays(1));
            Thing c = new Thing(3,'x',DateTime.Now.AddDays(-1));
            Thing d = new Thing(4,'w',DateTime.Now.AddDays(2));
            Thing e = new Thing(5,'v',DateTime.Now.AddDays(-2));
            
            ThingCollection tc = new ThingCollection();
            
            tc.AddRange(new Thing[]{a,b,c,d,e});
            
            Thing result;
    
            //Max by date
            result = tc.Aggregate(tc[0], 
                delegate (Thing Best, Thing Candidate) 
                { 
                    return (Candidate.theDateTime.CompareTo(
                        Best.theDateTime) > 0) ? 
                        Candidate : 
                        Best;  
                }
            );
            Console.WriteLine("Max by date: {0}", result.Dump());
            
            //Min by char
            result = tc.Aggregate(tc[0], 
                delegate (Thing Best, Thing Candidate) 
                { 
                    return (Candidate.theChar < Best.theChar) ? 
                        Candidate : 
                        Best; 
                }
            );
            Console.WriteLine("Min by char: {0}", result.Dump());               
        }
    }
    

    The results:

    Max by date: I: 4, S: w, D: 10/3/2008 12:44:07 AM
    Min by char: I: 5, S: v, D: 9/29/2008 12:44:07 AM

提交回复
热议问题