Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dmitriy 45 posts 216 karma points
    1 week ago
    Dmitriy
    0

    How to get content by index in Enumerable?

    I'm trying to get content by index from set, but it does't compile.

    Example:

    var set = Model.MyContent; // it is a NestedContent!
    for (int i = 0; i < set.Count(); i++)
     {
         <p>@set[i].MyProperty</p> // Error! 
     }
    

    Why?! Enumerable implements enumerable, so why I can't just use it?

    Does better way exists? All I want - render my content in rows like that :

    Row 1
    - item 1
    - item 2
    - item 3
    Row 2
    - item 4
    - item 5
    - item 6
    

    I tryed to use .Next() method, but it does,t works with NestedContent for some reason.

  • Alex Skrypnyk 3920 posts 13872 karma points mvp
    1 week ago
    Alex Skrypnyk
    0

    Hi Dmitriy

    What version of Umbraco are you using?

    "NestedContent" is it "IEnumerable"?

    Thanks,

    Alex

  • Alex Skrypnyk 3920 posts 13872 karma points mvp
    1 week ago
    Alex Skrypnyk
    1

    Try this code:

    var set = Model.MyContent.ToList();
    
  • Dmitriy 45 posts 216 karma points
    1 week ago
    Dmitriy
    0

    Thanks, Alex. It works!

  • Dan Diplo 1140 posts 4009 karma points mvp
    1 week ago
    Dan Diplo
    101

    Nested content is of type IEnumerable<IPublishedContent>. The IEnumerable interface doesn't support accessing items by index. In order to access by index you need to convert to either an Array or a List that does support addressing by index ie.

    var items = Model.MyContent.ToList();
    

    or

    var items = Model.MyContent.ToArray();
    

    However, it sounds like you want to break your content into equally sized groups? In that case, use the InGroupsOf() extension method. Something like:

    var groups = Model.MyContent.InGroupsOf(3);
    
    foreach (var group in groups)
    {
            <h4>Group</h4>
            <ul>
            @foreach (var item in group)
            {
                <li>@item.Name</li>
            }
            </ul>
    }
    
  • Dmitriy 45 posts 216 karma points
    1 week ago
    Dmitriy
    0

    Dan, Thanks a lot! That is exactly what I wanted! Yes, I realy wanted to group my content in equel groups and then render it!

    I'm using Bootstrap 3 without flexboxes, so it hard to render grid when you have different height of content.

    For other with same problem, I just leave solution here:

    <section>
        @foreach (var row in Model.MyContent.InGroupsOf(3))
        {
            <div class="row">
                @foreach (MyModel item in row)
                {
                    <div class="col-xs-12 col-md-4 col-sm-4">
                        <p>@item.MyProperty</p>
                    </div>
                }
            </div>
        }
    </section
    
Please Sign in or register to post replies

Write your reply to:

Draft