Возможно ли в foreach запихнуть два условия, что бы избежать foreach в foreach? Пример:
foreach(var t in ViewBag.News){
foreach(var n in ViewBag.Like){
}
}
это превратить в это:
foreach(var t in ViewBag.News && var n in ViewBag.Like){}
Весь код страницы:
@using WebPhotoArchive.Models
@model IQueryable<Post>
@{
ViewBag.Title = "Home Page";
string Name = null;
int id_link = 0;
}
@if (ViewBag.News == null)
{
<p><h3><b>Подпишитесь на других пользователей, чтобы получать их публикации!</b></h3></p>
}
else
{
foreach (var n in ViewBag.News)
{
<div class="row">
<br />
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="thumbnail">
@{ Name = n.UserDoName; id_link = n.UserDoId;}
<input type="hidden" name="id_post" value="@n.Id" />
<div class="collapse navbar-collapse">
<h4>@Html.ActionLink(Name, "/UserProfile/" + id_link, "Home", new { @class = "nav navbar-nav", @style = "text-decoration: none; color: #000000;" })</h4>
</div>
<hr />
@Html.Raw("<img src=\"data:image/jpeg;base64," + Convert.ToBase64String(n.Photo) + "\" />") @*для уменьшения добавить в img style='width:300px; height:350px;'*@
<p><strong>@Name:</strong><i>@n.Description</i></p>
<div class="btn-group" id="Div1">
<p>
<a class="btn-group" href="@Url.RouteUrl(new {controller = "Home", action = "LikeSystem", id = n.Id })">
@foreach (var t in ViewBag.LikePost)
{
if (n.Id == t.LikingId)
{
<img style='width:30px; height:30px;' src="~/Content/like_2.png" />
}
else
{
<img style='width:30px; height:30px;' src="~/Content/like_1.png" />
}
}
</a>
<a class="btn-group" href="@Url.RouteUrl(new {controller = "Home", action = "Comment", id_post = n.Id })">
<img style='width:30px; height:30px;' src="~/Content/comments.png" />
</a>
</p>
</div>
</div>
</div>
<br />
<div class="col-md-3"></div>
</div>
}
}
Контроллер:
public ActionResult Index()//новости
{
#region My Id, variable 'i' for get it
int i = 0;
var id = from c in db.Users where c.Login == User.Identity.Name select c.Id;
foreach (var t in id)
{
i = t;
}
#endregion
using (WebArchiveContext dbc = new WebArchiveContext())
{
var list_like = dbc.Likes.Where(l => l.LikerId == i);
ViewBag.LikePost = list_like;
}
var news_following_id = db.Follows.Where(u => u.FollowerId == i).Select(u => u.FollowingId);
IQueryable<Post> list_news = null;
list_news = db.Posts.Where(u => news_following_id.Contains(u.UserDoId)).OrderByDescending(p => p.Time);
if (list_news.Any())
{
ViewBag.News = list_news;
}
else ViewBag.News = null;
return View();
}
t
здесь?foreach(var t in ViewBag.News && var t in ViewBag.Like){}