Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 07:24
    Tom
    0

    Razor Nodes not contained in list

    Hi i was just wondering how you'd formulate the query for a list of nodes that doesn't already appear in another list..

    in linq it'd be something like

     

    var filteredList = someQuery.Where(item => !someExistingList.Any(l => l.ID == item.ID));

     

    how would I do the same?

    I have the following so far:

    @{
          int recommendedProductsCount = 5;
          dynamic recommendedProducts = new List<dynamic>();
        
          var productsOfSameType = Model.Up().Children.Where("nodeTypeAlias == \"product\"").Where("productType == " + @Model.productType);
          if(productsOfSameType != null && productsOfSameType.Items.Count > 0)
          {
            foreach(var item in productsOfSameType)
            {
              recommendedProdcts.Add(item);
              recommendedProductsCount--;
        
              if(recommendedProducts.Count == 2)
              {
                break;
              }      
            }
          }
        
          var remainingProductsInCollection = Model.Up().Children.Where(c => !recommendedProducts.Any(p => p.productID == c.productID));
  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 08:08
    Tom
    0

    Turns out I can't do this for some reason?

    var productsOfSameType Model.Up().Descendants("product").Where("productType == " @Model.productType);

     

    apparently I don't have access to where at that point? It is finding the private method for Where in

    DynamicNodeList and trying to execute

    that and throwing:

     

    umbraco.MacroEngines.DynamicNodeList.Where(string, params object[])' is inaccessible due to its protection level 

  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 08:26
    Tom
    0

    I can do things like:

    Model.Up().Descendants("product").Where("Name == \"Some Product Name\"")

    but the minute I try and use a property of the product DocType it doesn't work

     

  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 09:27
    Tom
    0

    Please see attached my complete code:

     @inherits umbraco.MacroEngines.DynamicNodeContext

    @helper cssClassForItem(int currentItemIndex)
    {
     @Html.Raw(String.Format(" class=\"block-left no-overflow{0}\""(currentItemIndex == " last" "")));
    }

    @{
      int recommendedProductsCount 5;
      dynamic recommendedProducts new List<dynamic>();

      var productsOfSameType Model.Parent.Children.Where("productType == \"Some product type\"");
      if (productsOfSameType != null &productsOfSameType.Items.Count 0)
      {
        foreach (var item in productsOfSameType)
        {
          recommendedProducts.Add(item);
          recommendedProductsCount--;

          if (recommendedProducts.Count == 2)
          {
            break;
          }
        }
      }

      var valuesForFilter new Dictionary<stringobject>();
      var productCodes new List<string>();

      foreach (var selectedProduct in recommendedProducts)
      {
        productCodes.Add(selectedProduct.Name);
      }

      if (productCodes.Count 0)
      {
        valuesForFilter.Add("productCodes"productCodes);
        var remainingProductsInCollection @Model.Up().Descendants("product").Where("!Name.ContainsAny(productCodes)"valuesForFilter);

        if (remainingProductsInCollection != null &remainingProductsInCollection.Items.Count 0)
        {
          var remainingProductsToAdd remainingProductsInCollection.Items.Count recommendedProductsCount remainingProductsInCollection.Items.Random(recommendedProductsCountremainingProductsInCollection.Items;
          recommendedProducts.AddRange(remainingProductsToAdd);
        }
      }

      if (recommendedProducts.Count 0)
      {
        int currentItemCount 1;
        

      <div id="collection-items-wrapper" class="clearfix">
        <ul class="list-none">
          @foreach (var product in recommendedProducts)
       {

            <li@cssClassForItem(currentItemCount)><href="@product.Url">
              <img src="/media/products/tn_@{@product.Name}.jpg" width="100" height="100" alt="@product.productDescription"/></a<span><href="@product.Url">@product.Name</a></span>
            </li>
         currentItemCount++;
       }
        </ul>
      </div>
      }
    }
    }

  • Alex 78 posts 136 karma points
    Jul 20, 2011 @ 09:37
    Alex
    0

    Tom, 

    Not looked at your full code but I can see a problem in this line:

    var productsOfSameType Model.Up().Descendants("product").Where("productType == " @Model.productType);

    First of all you don't need the @ in front of Model (you are already in "code mode"). Secondly, if Model.productType is a string you need to enclsoe the value in double quotes (like you would if you were placing static text). So something like this:

    var productsOfSameType Model.Up().Descendants("product").Where("productType == \"" @Model.productType + "\"");

    or

    var productsOfSameType Model.Up().Descendants("product").Where(String.Format("productType == \"{0}\"", @Model.productType));
  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 09:43
    Tom
    0

    Thanks for the reply Alex.. Sorry im still very green to this..

    I'm also getting Error loading Razor Script RelatedProducts.cshtml

    No applicable method 'ContainsAny' exists in type 'String

    when trying to do the filter on the 2nd lot..

     

     

     var valuesForFilter new Dictionary<stringobject>();
      var productCodes new List<string>();

      foreach (var selectedProduct in recommendedProducts)
      {
        productCodes.Add(selectedProduct.Name);
      }

      if (productCodes.Count 0)
      {
        valuesForFilter.Add("productCodes"productCodes);
        var remainingProductsInCollection @Model.Parent.Children.Where("!Name.ContainsAny(productCodes)"valuesForFilter);

        if (remainingProductsInCollection != null &remainingProductsInCollection.Items.Count 0)
        {
          var remainingProductsToAdd remainingProductsInCollection.Items.Count recommendedProductsCount remainingProductsInCollection.Items.Random(recommendedProductsCountremainingProductsInCollection.Items;
          recommendedProducts.AddRange(remainingProductsToAdd);
        }
      }

     


Please Sign in or register to post replies

Write your reply to:

Draft