Foreach in Foreach

0

I need to make 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 the category name only appears once, here's 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>
    
asked by anonymous 25.08.2015 / 22:05

2 answers

1

Friend, I did not understand the reason for linq, see if it helps:

Your 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>

    
26.08.2015 / 15:55
0

You need a category x gallery, so you would make a foreach first in the category and then in the galleries list of the category, which would be an attribute of the category. IEnumerable

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

    
25.08.2015 / 22:15