Inline lambda - get rate with latest date
The MVC ViewPage contains Users (intranet.Model.User). Each user may have one or more rates of pay represented in the model as intranet.model.User.UserRates.
I need to be able to display the latest rate of pay per user using an inline Lambda expression but I am having no success (an error message is displayed rather than the rate of pay).
My latest incarnation is:
<% var latestRate = item.UserRates
.GroupBy(hr=>hr.rate)
.Select(g=>g.Single(
d=>d.effective == g.Max(m=>m.effective)开发者_JAVA技巧)
); %>
<%= Html.Encode(latestRate) %>
</td>
Error message in column is:
System.Linq.Enumerable+WhereSelectEnumerableIterator
2[System.Linq.IGrouping
2[System.Decimal,intranet.Models.UserRate],intranet.Models.UserRate]
I can't figure out exactly what your data looks like, but couldn't you do something like this?
var latestRate = item.UserRates
.OrderByDescending(x => x.effective)
.First();
Final iteration (for now):
<% var userRate = item.UserRates
.OrderByDescending(hr=>hr.effective)
.FirstOrDefault() ?? new intranet.Models.UserRate(); %>
<td class="hrlyRate">
<%= Html.Encode(string.Format("{0:0.000}", userRate.rate))%>
</td>
精彩评论