Copied to clipboard

Flag this post as spam?

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


  • Michael Beever 74 posts 155 karma points
    May 22, 2020 @ 14:26
    Michael Beever
    0

    ToList help

    Hi,

    I am trying to create a list

    var newsposts = selection.OrderByDescending(x => x.CreateDate).Where(x => x.NewType2 == @PageTheme).ToList();
    

    I am trying to only pull certain content out. Currently using an if statement further down the page but this breaks pagniation

    Is there was I can exclude the content?

    Thanks

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    May 23, 2020 @ 19:29
    Anders Bjerner
    0

    Hi Michael,

    Can you perhaps share a bit more of your code?

    Ideally you should wait converting the collection to a list until you're done manipulating your collection - eg. after you have applied pagination. Otherwise you may end up converting to a list multiple times.

  • Michael Beever 74 posts 155 karma points
    May 26, 2020 @ 07:17
    Michael Beever
    0

    string PageTheme = Model.Content.GetPropertyValue

    var selection = Umbraco.TypedContent(1999).Children("mOPNewsPost").Where(x => x.IsVisible()).OrderBy("CreateDate");
    
    var numberOfPosts = Model.Content.GetPropertyValue<int>("newsItems");
    
        var newsposts = selection.OrderByDescending(x => x.CreateDate).Where(x => x.NewType2 == @PageTheme).ToList();
    
        var pageCount = (int)Math.Ceiling((double)newsposts.Count / (double)numberOfPosts);
            var page = 1;
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                int.TryParse(Request.QueryString["page"], out page);
                if (page <= 0 || page > pageCount)
                {
                    page = 1;
                }
            }
            //Gets the blogposts for the current page
            var pagedNewsposts = newsposts.Skip((page - 1) * numberOfPosts).Take(numberOfPosts).ToList();
    

    }

      @foreach(var item in pagedNewsposts){ var PageType = item.GetPropertyValue
            </div>
    
    
       <div class="col-md-2">
    <h4>Filter</h4>
    
    
    
    <a href="\news\" class="badge badge-success">All Items</a>
    <a href="?NewsType=News" class="badge badge-primary">News</a>
    <a href="?NewsType=Reports" class="badge badge-secondary">Reports</a>
    
    <a href="?NewsType=Tax Changes" class="badge badge-danger">Tax Changes</a>
    <a href="?NewsType=Regulation Changes" class="badge badge-warning">Regulation Changes</a>
    <a href="?NewsType=LGPS Reform" class="badge badge-info">LGPS Reform</a>
    
    
    
    </div>
    @{
     if (newsposts.Count > numberOfPosts)
    {
    
    <div class="col-md-12">
    
            <nav class="navbar navbar-expand-lg justify-content-md-center">
    
                @if (page <= 1)
                {
                    <span class="nav-link nav-link--black nav-link--disabled">Prev</span>
                }
                else
                {
                    <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page - 1))">Prev</a>
                }
    
                @for (int i = 1; i <= pageCount; i++)
                {
                    <a class="nav-link nav-link--black @(page == i ? "nav-link--active" : null)" href="@(Model.Content.Url + "?page=" + i)">@i</a>
                }
                @if (page == pageCount)
                {
                    <span class="nav-link nav-link--black nav-link--disabled">Next</span>
                }
                else
                {
                    <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page + 1))">Next</a>
                }
    
            </nav>
    
    </div>}
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft