0
I need to do a foreach inside another but are two tables one is called categories and the other is a galerya, I need to return all galeryas within each category only that the name of the category only appears once, here is my code
(var db = new DBContext())
{
//Buscando as categorias ativas
List<Category> listCategory;
listCategory = (from ct in db.ses.Query<Category>()
join g in db.ses.Query<Galery>() on ct.id equals g.idCategory
where ct.status == 1
select ct).ToList();
//Buscando as galeryas ativas
var list = (from g in db.ses.Query<Galery>()
join ct in db.ses.Query<Category>() on g.idCategory equals ct.id
where g.status == 1
&& g.idCategory == idCategory
orderby g.id descending
select new IndexClass { idCategory = ct.id, title = g.title }).ToList().Take(3);
ViewBag.listCategory = listCategory.Distinct();
return View(list);
}
I have a generic class
public class IndexClass
{
public virtual int idCategory { get; set; }
public virtual string nameCategory { get; set; }
public virtual string title { get; set; }
}
and this is my view
<div class="main">
<!--=====================Content======================-->
<section class="content cont_pad1">
<div class="container"><div class="ic">More Website Templates TemplateMonster.com - May 05, 2014!</div>
<div class="gallery gall__1">
<div class="row">
@foreach (var itemCategory in ViewBag.listCategory)
{
<div class="grid_4">
<h3>@itemCategory.title</h3>
</div>
<div class="clear"></div>
foreach (var item in Model)
{
<div class="grid_4">
<a href="/Content/images/uploads/galery/full1.jpg" class="gal_item">
<img src="/Content/images/uploads/galery/page2_img1.jpg" alt="">
<div class="gal_caption">
<time datetime="2014-01-01">@item.title</time>
</div>
<span class="gal_magnify"></span>
</a>
</div>
}
}
</div>
</div>
</div>
</section>
</div>
João Vitor, you could remove the first query and use a group g by ct in the second to group the galleries by category.
– Tobias Mesquita