Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a pair of extension methods that could help you out: ObjectWithMin and ObjectWithMax:</p> <pre><code>public static T ObjectWithMax&lt;T, TResult&gt;(this IEnumerable&lt;T&gt; elements, Func&lt;T, TResult&gt; projection) where TResult : IComparable&lt;TResult&gt; { if (elements == null) throw new ArgumentNullException("elements", "Sequence is null."); if (!elements.Any()) throw new ArgumentException("Sequence contains no elements."); var seed = elements.Select(t =&gt; new {Object = t, Projection = projection(t)}).First(); return elements.Aggregate(seed, (s, x) =&gt; projection(x).CompareTo(s.Projection) &gt;= 0 ? new {Object = x, Projection = projection(x)} : s ).Object; } public static T ObjectWithMin&lt;T, TResult&gt;(this IEnumerable&lt;T&gt; elements, Func&lt;T, TResult&gt; projection) where TResult : IComparable&lt;TResult&gt; { if (elements == null) throw new ArgumentNullException("elements", "Sequence is null."); if (!elements.Any()) throw new ArgumentException("Sequence contains no elements."); var seed = elements.Select(t =&gt; new {Object = t, Projection = projection(t)}).First(); return elements.Aggregate(seed, (s, x) =&gt; projection(x).CompareTo(s.Projection) &lt; 0 ? new {Object = x, Projection = projection(x)} : s ).Object; } </code></pre> <p>These two are more efficient than OrderBy().FirstOrDefault() when used on in-memory collections; however they're unparseable by IQueryable providers. You'd use them something like this:</p> <pre><code>ts = context.Documents .Where(r =&gt; r.CustomerID == ThisCustomerID) .ObjectWithMax(r=&gt;r.m_Timestamp); </code></pre> <p><code>ts</code> is now the object having the most recent timestamp, so you don't need a second query to get it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload