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));
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));
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
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
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 % 5 == 0 ? " 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<string, object>();
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(recommendedProductsCount) : remainingProductsInCollection.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)><a href="@product.Url">
<img src="/media/products/tn_@{@product.Name}.jpg" width="100" height="100" alt="@product.productDescription"/></a> <span><a href="@product.Url">@product.Name</a></span>
</li>
currentItemCount++;
}
</ul>
</div>
}
}
}
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));
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<string, object>();
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(recommendedProductsCount) : remainingProductsInCollection.Items;
recommendedProducts.AddRange(remainingProductsToAdd);
}
}
is working on a reply...