Our customer is able to choose up to 3 items to show using MNTP, all items are under same parent. If they only choose 2 the idea is that we should take 1 by random that is not already represented. To do this im taking the nodes the customer has already choosen and putting them into a list of dynamic.
Then im going through all the children(including the already selected items) and here I want to check if it occurs in my list, if not i wanna add it.
its my Where clause thats not working
int rest = 3;
List<dynamic> products = new List<dynamic>();
@*Sorting to see if we need to pickup random items*@
foreach (var item in @Model.productBox1Products)
{
var node = Model.NodeById(item.InnerText);
products.Add(node);
rest--;
}
@*Taking parent of The first item to take random items from*@
if (rest != 0)
{
dynamic parent = @products[0].Parent;
foreach (var item in parent.DescendantsOrSelf("product").Where(x => !products.Contains(x)))
{
<p>@item.Name</p>
}
}
Just to clarify. My customer has the options to select between 1-3 products using the Multi node tree picker.
Lets say they only select 1 product, that means we want to 2 random products from the same category that is NOT the already selected one and is random.
So ive created to functions you can throw into your razor file and then you can call the getItemsNotInListInRandomOrder function. The other function is a private one used to take a list and return it with all the items in a random order. Its based on the Fisher-Yates implementation.
The function takes
Parent : its only used in the first line to get all products from the category as a list. Then it takes a
Products : its a list of the already selected products in this situation it will only contain 1 product.
Rest : is just an int saying how many items we want (when rest reaches 0 it breaks and returns the list.)
Hope it helps you.
@functions{ //get randoms not in list public List<dynamic> getItemsNotInListInRandomOrder(dynamic parent, List<dynamic> products, int rest) {
dynamic list = parent.DescendantsOrSelf("product"); List<dynamic> allProductsUnderGroup = new List<dynamic>(); foreach (var item in list) { allProductsUnderGroup.Add(item); }
check if item is not in a list using where clause
Hey there
Our customer is able to choose up to 3 items to show using MNTP, all items are under same parent. If they only choose 2 the idea is that we should take 1 by random that is not already represented. To do this im taking the nodes the customer has already choosen and putting them into a list of dynamic.
Then im going through all the children(including the already selected items) and here I want to check if it occurs in my list, if not i wanna add it.
its my Where clause thats not working
Ive figured it out but a bit over the top solution.
what was your solution even if it was over the top?
Arh sry for not posting here it is :)
Just to clarify. My customer has the options to select between 1-3 products using the Multi node tree picker.
Lets say they only select 1 product, that means we want to 2 random products from the same category that is NOT the already selected one and is random.
So ive created to functions you can throw into your razor file and then you can call the getItemsNotInListInRandomOrder function. The other function is a private one used to take a list and return it with all the items in a random order. Its based on the Fisher-Yates implementation.
The function takes
Parent : its only used in the first line to get all products from the category as a list. Then it takes a
Products : its a list of the already selected products in this situation it will only contain 1 product.
Rest : is just an int saying how many items we want (when rest reaches 0 it breaks and returns the list.)
Hope it helps you.
is working on a reply...