Answer the question
In order to leave comments, you need to log in
How not to get the same object twice?
There is a view, in the model of which I get all sections of the forum. Then I go through all the sections and display the last 3 topics for each section, and each of the 3 topics has the date of the last message and the name of the author.
@foreach (AuthorizationNew.Models.Section sc in Model)
{
<div id="ThemeName">@Html.ActionLink(@sc.SectionName, "GetSection",
new { idSection = @sc.Id })</div>
foreach (AuthorizationNew.Models.Theme th in
sc.Theme.OrderByDescending(m => m.ThemeDate).Take(3).ToList())
{
<div class="Thems">
@Html.ActionLink(@th.ThemeName, "GetTheme", new { idTheme = @th.ThemeId })
@if (th.FMessage.Count > 0)
{
@th.FMessage.OrderByDescending(m => m.ForumMessageId).FirstOrDefault().MessageDate
@:От @th.FMessage.OrderByDescending(m => m.ForumMessageId).FirstOrDefault().User.DisplayName
}
</div>
}
}
@th.FMessage.OrderByDescending(m => m.ForumMessageId).FirstOrDefault().MessageDate
@:От @th.FMessage.OrderByDescending(m => m.ForumMessageId).FirstOrDefault().User.DisplayName
I filter the list twice and get the same last object. How can this part be optimized without moving the element selection logic to the controller?
Answer the question
In order to leave comments, you need to log in
Use the @{ } construct, and if you need to put text there, then use . And no one forbids declaring variables.
@if (th.FMessage.Count > 0)
{
var message = th.FMessage.OrderByDescending(m => m.ForumMessageId).FirstOrDefault();
@message.MessageDate
<text>От</text> @message.User.DisplayName
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question