Copied to clipboard

Flag this post as spam?

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


  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 17:24
    bob baty-barr
    0

    Skip and Take issue with Examine Razor Script

    i am getting the following error when trying to use skip and take with examine and razor when saving...

    'System.Collections.Generic.IEnumerable' has no applicable method named 'Skip' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax'

    here are my using statements...

    @using Examine;
    @using Examine.LuceneEngine.SearchCriteria;
    @using Examine.SearchCriteria;
    @using UmbracoExamine;
    @using System.Linq;
    @using System.Xml.Linq;

    oh, and here is my statement...

    @foreach (var c in results.Skip(currentPage*itemsPerPage).Take(itemsPerPage)
              )
  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jul 29, 2011 @ 17:54
    Tim
    0

    Are you using the latest version of Examine? I had to get the new version from codeplex to get skip and take to work properly for me, but now that I have it works mint!

    :)

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 17:55
    bob baty-barr
    0

    hm.... the site is a 4.7 site, but i will check on the latest version issue...

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:02
    Tom Madden
    0

    Bob,

    This works for me:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
       
       
        var news_items = @Model.AncestorOrSelf().Descendants("Event");
    }
    @foreach (var news_item in news_items.Skip(10).Take(4))
    {
    }

    That's all my using statements, for this test. You might need to look at how your results variable is being assigned

    Tom

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 18:04
    bob baty-barr
    0

    i can get it to work with hard coding numbers into the Skip and Take but as soon as i introduce variables, i get the error??? any thoughts?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jul 29, 2011 @ 18:28
    Morten Bock
    0

    Maybe it is related to how those variables are declared? Can you show the whole snippet?

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 18:30
    bob baty-barr
    0

    here it is in all it's glory ;) -- remember I AM A HACK :)

    @using Examine;
    @using Examine.LuceneEngine.SearchCriteria;
    @using Examine.SearchCriteria;
    @using UmbracoExamine;
    @using System.Linq;
    @using System.Xml.Linq;

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext






    @* Get the search term from query string *@
    @{var searchTerm = Request.QueryString["q"];}

    @{ // configuration
      var itemsPerPage = String.IsNullOrEmpty(Parameter.ItemsPerPage) ? 10 : int.Parse(Parameter.ItemsPerPage);
      var previousLabel = String.IsNullOrEmpty(Parameter.PreviousLabel) ? "Prev" : Parameter.PreviousLabel;
      var nextLabel = String.IsNullOrEmpty(Parameter.NextLabel) ? "Next" : Parameter.NextLabel; }


    @{ var sc = ExamineManager.Instance.CreateSearchCriteria(global::Examine.SearchCriteria.BooleanOperation.Or);

                var terms = searchTerm.Split(' ');
      
      var query = sc
                            .Field("bodyText", searchTerm.MultipleCharacterWildcard())
                            .Or()
                            .Field("pageHeading", searchTerm.MultipleCharacterWildcard())
                            .Or()
                            .Field("FileTextContent", searchTerm.MultipleCharacterWildcard());

                foreach (var term in terms)
                {
                    query = query.Or().NodeName(term);
                }
      
      
      
      }

    @{var results = ExamineManager.Instance.SearchProviderCollection["FrontSiteSearcher"].Search(query.Compile())
                    .Concat(ExamineManager.Instance.SearchProviderCollection["PDFSearcher"].Search(query.Compile()));}

    @{var TotalResults = results.Count();}

    @{var pageResults = results;}


    @{// paging calculations
      var numberOfItems = TotalResults;
      int currentPage = 1;
      if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage)) {
        currentPage = 1;
      }
      currentPage--;
      }

     @{ var numberOfPages = numberOfItems % itemsPerPage == 0 ? Math.Ceiling((decimal)(numberOfItems / itemsPerPage)) : Math.Ceiling((decimal)(numberOfItems / itemsPerPage))+1;}


    <p>Your search for <strong>@searchTerm</strong> returned <strong>@TotalResults</strong> results.</p>

    @foreach (var c in results.Skip(currentPage*10).Take(10)
              )
        
    {            
           <h4>
             @if(c.Fields.Keys.Contains("umbracoFile"))
            {
             <a href="@c.Fields["umbracoFile"]" target="_blank">
                 @c.Fields["nodeName"]
               </a>
             }
               else
             {
               <a href="@umbraco.library.NiceUrl(c.Id)">
                 @c.Fields["nodeName"]
               </a>
              }
           </h4>
             
              
                
            if(c.Fields.Keys.Contains("bodyText"))
            {
              var bodyText = c.Fields["bodyText"];
              if (bodyText.Length>300)
              {
                bodyText = bodyText.Substring(0,300);
              }
              <p class="searchResult">@Html.Raw(bodyText)</p>
            }
               else if(c.Fields.Keys.Contains("FileTextContent"))
            
     
             {
              var pdfText = c.Fields["FileTextContent"];
              if (pdfText.Length>300)
              {
                pdfText = pdfText.Substring(0,300);
              }
              <p class="searchResult">@Html.Raw(pdfText)</p>
              
              }
                     
    }



    @{
      var pagesToList = pageResults;

      

      

     <!-- <p>
        Total Items: @numberOfItems <br />
        Items per Page: @itemsPerPage<br />
        Pages: @numberOfPages;<br />
        Current Page: @(currentPage)
      </p> -->

      

      <div id="searchNav">
        @{
      // Google style paging links
        if (currentPage > 0) {
          <a href="?q=@searchTerm&page=@(currentPage)" class="prev">&laquo; @previousLabel</a>
        } else {
          <span class="pagingDisabled">&laquo; @previousLabel</span>
        }
        
        var Pages = Enumerable.Range(1, (int)numberOfPages);
        foreach(var number in Pages) {
          if (number-1 != currentPage) {
          <a href="?q=@searchTerm&page=@number">@number</a>
          } else {
          <a href="?q=@searchTerm&page=@number" class="current">@number</a>
          }
          
        }

        if (currentPage < Pages.Count()-1) {
          <a href="?q=@searchTerm&page=@(currentPage+2)" class="next">@nextLabel &raquo;</a>
        } else {
          <span class="pagingDisabled">@nextLabel &raquo;</span>
        }
      }
      </div>
    }
  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:31
    Tom Madden
    0

    Have you traced the code to see their values?

    This works for me (ignore the fact I'm using page 0)

      var currentPage = 0;
        var itemsPerPage = 100;   
       
    }
    @foreach (var news_item in news_items.Skip(currentPage*itemsPerPage).Take(itemsPerPage))
    }
  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:36
    Tom Madden
    0

    Have you set itemsPerPage as a parameter in your Macro, and have you givien it a value when inserting the macro?

    If you've VS Pro version, add a breakpoint and attach the debugger to your IIS and check the value of itemsPerPage.

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 18:38
    bob baty-barr
    0

    yeah, i have no idea how to scope variables, etc. with this junk... and all the curly braces... like i said... I AM A TOTAL HACK :(

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:43
    Tom Madden
    0

    Have you got VS PRo and IIS running locally? Have you tried stepping through the code? If you've got these 2 things, you can skype me and we can talk through it.

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jul 29, 2011 @ 18:46
    bob baty-barr
    0

    oh, Tom, Tom, Tom... you know i am editing this razor script live on the server ;)

    i declared a new variable right before the for-each and it is workign as expected... so basically, i have WAY TOO MANY curly braces all over the place that screw up the scope of some variables...

    i will learn eventually ;)

    thanks for ALL your help!

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:52
    Tom Madden
    0

    Bob,

    where you've listed the whole script above, you hard coded the page size. Does the error still exist? If it does, the problem lies with the value of currentPage. See what value that has. If you don't want to attach the debugger, hard code it to a one, then just output the value to see what it is. Surround it with something you can quickly find in the output.

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Jul 29, 2011 @ 18:56
    Tom Madden
    1

    At the moment, working with Razor's is painful, mostly due (IMHO) to the fact that the MS compiler isn't very good. It's a v1 after all. Often the error message you get are way off, and usually it's related to mistmatched braces, or forgetting when you need to put in an @ symbol or a code block. Roll on Umbraco 4.7.1, and Razor 1.5sp1.

  • Manish 373 posts 932 karma points
    Jan 29, 2013 @ 09:25
    Manish
    0

    I have a node in XML with the name "productPageTitle" and I dont want this node to be a part of search result.

    <ExcludeNodeTypes>
          <add Name="titleProperties"/>
        </ExcludeNodeTypes>

    So I used <ExcludeNodeType> but still as a part of result.

    How can I exclude this node from search results?

  • Tatiana Vassilieva 18 posts 98 karma points
    Apr 23, 2013 @ 02:15
    Tatiana Vassilieva
    0

    This dates quite a while back, wasn't the whole issue just the fact that Skip() and Take() need an int value?
    ie. int number = Convert.ToInt32(@yourValue);

Please Sign in or register to post replies

Write your reply to:

Draft