I have two lists of model types that are initialized in the controller and values are inserted into them. These list objects are then assigned to ViewData so that I can iterate through them using a foreach loop. However, the issue is that the models of these two lists have a 'Time' property, and I want to print these two lists ordered by their time values. Here is my code:
Model of List1:
public class Likes
{
public string UserName { get; set; }
public string UserPhoto { get; set; }
public int UserId { get; set; }
public string PostText { get; set; }
public int? PId { get; set; }
public DateTime? Liketime { get; set; }
}
Model of List2:
public class Comments
{
public string UserName { get; set; }
public string UserPhoto { get; set; }
public int UserId { get; set; }
public string CommentText { get; set; }
public int? PId { get; set; }
public DateTime? Commenttime { get; set; }
}
Controller:
public ActionResult Home()
{
List<Likes> List = new List<Likes>();
var f = (from n in _db.PostLikes where n.ToWhomId == CurrentUser.User.Id join o in _db.Users on n.UserId equals o.Id join p in _db.Posts on n.PostId equals p.Id select new { Name = o.UserName, Photo = o.UserPhoto, ID = n.UserId, PID = n.PostId ,p.PostText,n.Time }).ToList().OrderByDescending(n=>n.Time);
foreach (var item in f)
{
Likes mylikes = new Likes()
{
PId = item.PID,
UserName = item.Name,
UserPhoto = item.Photo,
Posttext=item.PostText,
Liketime=item.Time
};
List.Add(mylikes);
}
ViewData["likes"] = List;
List<Comments> List2 = new List<Comments>();
var qw = (from n in _db.PostComments where n.ToWhomId == CurrentUser.User.Id join o in _db.Users on n.UserId equals o.Id join p in _db.Posts on n.PostId equals p.Id select new { Name = o.UserName, Photo = o.UserPhoto, ID = n.UserId, PID = n.PostId, p.PostText, n.Time, Comment = n.Comments }).ToList().OrderByDescending(n => n.ID);
foreach (var item in qw)
{
Comments mycomments = new Comments()
{
PId = item.PID,
UserName = item.Name,
UserPhoto = item.Photo,
CommentText = item.PostText,
Commenttime = item.Time
};
List2.Add(mycomments);
}
ViewData["comments"] = List2;
return View(posts);
}
View:
foreach (var item in (dynamic)ViewData["likes"])
{
if (item.Posttext == null)
{
var trim = item.Posttext.Substring(0, 5) + "...";
item.Posttext = "";
}
else
{
var trim = item.Posttext + " ";
item.Posttext = trim;
}
<li>
<a href="~/Profile/Index">
<img class="img-responsive center-block" src="~/Images/@item.UserPhoto" width="40" height="40" />
<p style="color:black"><b>@item.UserName</b> liked your post @item.Posttext </p>
<img width="16" height="16" src="@Url.Content("~/Images/like-128.png")" />
<span style="color:black"> @item.Liketime</span>
</a>
</li>
}
foreach (var item in (dynamic)ViewData["comments"])
{
// trim here
<li>
<a href="~/Profile/Index">
<img class="img-responsive center-block" src="~/Images/@item.UserPhoto" width="40" height="40" />
<p style="color:black"><b>@item.UserName</b> commented on your post </p>
<img width="16" height="16" src="@Url.Content("~/Images/Custom-Icon-Design-Flatastic-1-Comment (1).ico")" />
<span style="color:black"> @item.Commenttime</span>
</a>
</li>
}
However, I do not want to print List2
after List1
. Instead, I want to print them based on their respective Time
properties. If a model from List1
was inserted first, it should be printed first, followed by models from List2
until the end.