Copied to clipboard

Flag this post as spam?

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


  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Aug 01, 2012 @ 15:50
    Rasmus Fjord
    0

    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 

     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>
            } 
    }
  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Aug 02, 2012 @ 09:32
    Rasmus Fjord
    0

    Ive figured it out but a bit over the top solution.

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Aug 03, 2012 @ 12:05
    Ravi Motha
    0

    what was your solution even if it was over the top?

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Aug 03, 2012 @ 12:14
    Rasmus Fjord
    0

    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.


    @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);
    }

    allProductsUnderGroup = Shuffle(allProductsUnderGroup);

    foreach (var item in allProductsUnderGroup)
    {
    if (rest == 0)
    {
    break;
    }
    if (!products.Exists(x => x.Id == item.Id))
    {
    products.Add(item);
    rest--;
    }
    }


    return products;
    }
    /*Fisher-Yates shuffle implementation*/
    private List<dynamic> Shuffle(List<dynamic> list)
    {
    Random rng = new Random();
    int n = list.Count;
    while (n > 1)
    {
    n--;
    int k = rng.Next(n + 1);
    dynamic value = list[k];
    list[k] = list[n];
    list[n] = value;

    }
    return list;
    }
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft