What is the easiest way to return more than two numbers?

Let's say I have an entity that looks like this:

public class Album()
{
    public DateTime LastUpdated { get; set; }
    public List<Picture> Pictures { get; set; }
}

I want to create a property LastActivitythat returns the last action date. This is easy enough for a collection Pictures:

public DateTime LastActivity
{
    get { return Pictures.Max(x => x.LastUpdated); }
}

However, I also want to consider a property LastUpdatedin an object Album. I could use this code:

public DateTime LastActivity
{
    get { return Pictures.Max(x => x.LastUpdated) > this.LastUpdated
              ? Pictures.Max(x => x.LastUpdated)
              : this.LastUpdated) };
}

But this is bad because he will do the conversion Max()twice. Is there a better way to write this code?

Answer

This is the solution I came across based on the accepted answer:

public virtual DateTime LastActivity
{
    get
    {
        var max = Pictures.Any() ? Pictures.Max(x => x.LastUpdated) : DateTime.MinValue;
        return max > this.LastUpdated ? max : this.LastUpdated;
    }
}

You should ensure that if you do Max()in an empty collection, you will get an exception, so you need to check if there is anything in the collection in the first place.

+3
9

max , .

public DateTime LastActivity
{
    get 
    { 
        var max = Pictures.Max(x => x.LastUpdated);
        return max > this.LastUpdated
          ? max
          : this.LastUpdated 
    };
}
+7

, , .

+2
public DateTime LastActivity
{
    get
    {
        var picturesMax = Pictures.Max(x => x.LastUpdated);
        return picturesMax > this.LastUpdated
          ? picturesMax
          : this.LastUpdated)
    };
}
+1

?

public DateTime LastActivity 
{ 
    get 
    {
         DateTime lastPicture = Pictures.Max(x => x.LastUpdated);
         return lastPicture > this.LastUpdated ? lastPicture : this.LastUpdated;
     }
} 
+1
public DateTime LastActivity
{         
    get { return Pictures.Select(x => x.LastUpdated).Concat(new DateTime[] { this.LastUpdated }).Max();
}
+1

...

public static T Maximum<T>(params T[] values) 
    where T : struct, IComparable<T>
{
    var rV = values[0];
    foreach (var v in values.Where
        (v => v.CompareTo(rV) > 0))
        rV = v;
    return rV;
}

var maxVal = lib.Maximum(Value1, Value2, .... , ValueN);

0

:

public DateTime LastActivity
{
    get {
        DateTime maxLastUpdated = Pictures.Max(x => x.LastUpdated);
        return maxLastUpdated > this.LastUpdated
            ? maxLastUpdated
            : this.LastUpdated) };
}
0

LINKy...

public DateTime LastActivity
{
    get 
    { 
         return Pictures.Any(x => x.LastUpdated > this.LastUpdated) 
                ? Pictures.Max(x => x.LastUpdated) 
                : this.LastUpdated; 
    }
}

Update (s): Added the LastUpdated default value, which will be returned if the LINQ expression does not return any results and fixed for the OP comment.

0
source

This is pretty straight forward and should accept an empty collection Pictures:

public DateTime LastActivity
{
    get
    {
        return Pictures.Aggregate(LastUpdated,
                            (a, x) => x.LastUpdated > a ? x.LastUpdated : a);
    }
}
0
source

Source: https://habr.com/ru/post/1777761/


All Articles