tags:

views:

2139

answers:

4

Hi,

I have a list of objects that have to int properties. The list is the output of anothre linq query. The object:

 public class DimensionPair  {

  public int Height { get; set; }
  public int Width { get; set; }

I want find and return the object in the list which has the largest Height property value.

I can manage to get the highest value of the Height value but not the object itself.

Can I do this with Linq? How?

+12  A: 

This would require a sort (O(n log n)) but is very simple and flexible. Another advantage is being able to use it with LINQ to SQL:

var maxObject = list.OrderByDescending(item => item.Height).First();

Note that this has the advantage of enumerating the list sequence just once. While it might not matter if list is a List<T> that doesn't change in the meantime, it could matter for arbitrary IEnumerable<T> objects. Nothing guarantees that the sequence doesn't change in different enumerations so methods that are doing it multiple times can be dangerous (and inefficient, depending on the nature of the sequence). However, it's still a less than ideal solution for large sequences. I suggest writing your own MaxObject extension manually if you have a large set of items to be able to do it in one pass without sorting and other stuff whatsoever (O(n)):

static class EnumerableExtensions {
    public static T MaxObject<T,U>(this IEnumerable<T> source, Func<T,U> selector)
      where U : IComparable<U> {
       if (source == null) throw new ArgumentNullException("source");
       bool first = true;
       T maxObj = default(T);
       U maxKey = default(U);
       foreach (var item in source) {
           if (first) {
                maxObj = item;
                maxKey = selector(maxObj);
                first = false;
           } else {
                U currentKey = selector(item);
                if (currentKey.CompareTo(maxKey) > 0) {
                    maxKey = currentKey;
                    maxObj = item;
                }
           }
       }
       if (first) throw new InvalidOperationException("Sequence is empty.");
       return maxObj;
    }
}

and use it with:

var maxObject = list.MaxObject(item => item.Height);
Mehrdad Afshari
Rats, didn't see this before I posted. That's basically what we do in MoreLINQ, except we iterate directly using GetEnumerator rather than having a "first" flag. I think it makes the code a bit simpler. We also allow use Comparer<U> and allow one to be passed in, rather than requiring U to implement IComparable, but that's a side issue.
Jon Skeet
The last part is a lot of unnecessary complexity for a simple problem.
KristoferA - Huagati.com
This is a solution for a more generic problem. Basically, you declare such an extension method to *abstract away* the complexity whenever you need it.
Mehrdad Afshari
To paraphrase yourself, "I don't know what's the relation between this problem and abstracting away as a generic solution...". :) Sometimes stackoverflow is like a ham radio club...
KristoferA - Huagati.com
Isn't that how software development works?
Mehrdad Afshari
Like a ham radio club? Yep.
KristoferA - Huagati.com
A: 

I believe that sorting by the column you want to get the MAX of and then grabbing the first should work. However, if there are multiple objects with the same MAX value, only one will be grabbed:

private void Test()
{
    test v1 = new test();
    v1.Id = 12;

    test v2 = new test();
    v2.Id = 12;

    test v3 = new test();
    v3.Id = 12;

    List<test> arr = new List<test>();
    arr.Add(v1);
    arr.Add(v2);
    arr.Add(v3);

    test max = arr.OrderByDescending(t => t.Id).First();
}

class test
{
    public int Id { get; set; }
}
regex
+7  A: 

Doing an ordering and then selecting the first item is wasting a lot of time ordering the items after the first one. You don't care about the order of those.

Instead you can use the aggregate function to select the best item based on what you're looking for.

var maxHeight = dimensions.Aggregate((agg, next) => next.Height > agg.Height ? next : agg);

var maxHeightAndWidth = dimensions.Aggregate((agg, next) => next.Height >= agg.Height && next.Width >= agg.Width ? next: agg);
Cameron MacFarland
+8  A: 

We have an extension method to do exactly this in MoreLINQ. You can look at the implementation there, but basically it's a case of iterating through the data, remembering the maximum element we've seen so far and the maximum value it produced under the projection.

In your case you'd do something like:

var item = items.MaxBy(x => x.Height);

This is better (IMO) than any of the solutions presented here other than Mehrdad's second solution (which is basically the same as MaxBy):

  • It's O(n) unlike the currently accepted answer which finds the maximum value on every iteration (making it O(n^2))
  • The ordering solution is O(n log n)
  • Taking the Max value and then finding the first element with that value is O(n), but iterates over the sequence twice. Where possible, you should use LINQ in a single-pass fashion.
  • It's a lot simpler to read and understand than the aggregate version, and only evaluates the projection once per element
Jon Skeet
Isn't that what my solution does?
Cameron MacFarland
Nevermind, I read your last bullet point now.
Cameron MacFarland