Foreach in Foreach

Asked

Viewed 636 times

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.

2 answers

1

Friend, I don’t understand the reason of the explosion, see if it helps:

His action

public ActionResult Categorias()
    {
        var list = db.Category.Include("Galery").Where(ct => ct.status == 1).ToList();
        return View(list);
    }

Your View

@model List<Category>
<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 Model)
                {
                    <div class="grid_4">
                        <h3>@itemCategory.title</h3>
                    </div>
                    <div class="clear"></div>

                    foreach (var item in Model.Galery.Where(g=>g.status == 1).Take(3))
                    {
                        <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>

  • he’s using NHibernate and not Entity Framework, then in place of a Include you need to make a Fetch, for example: db.ses.Query<Category>().Fetch(ct => ct.Galery)

0

You need a category x gallery template, then you would hold a foreach first in the category and then in the category list of galleries, which would be a category attribute. Ienumerable

Another solution would be to create a method to return the list of linked galleries, but this would not have performance, because at each iteration would be a consumption to the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.