x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar558posts671karma

    No applicable method 'ContainsAny' exists in type 'String

    Tom started this topic July 21, 2011 @ 12:40, Go directly to the topic solution

    I was just wondering in this instance why the extension method wasn't being picked up.. and for now is there a workaround:

     

    No applicable method 'ContainsAny' exists in type 'String

    when trying to do the filter on @Model.Parent.Children in the line below where I'm setting the var: remainingProductsInCollection..

     

    Is anyone aware of a way to work around this?

     

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

     


  • Avatar135posts301karma
    Comment with ID: 84184
    Gareth Evans posted this reply July 21, 2011 @ 12:50

    Does *this* work:

    var remainingProductsInCollection = @Model.Parent.Children.Where("Name.ContainsAny(productCodes)", valuesForFilter);

    The reverse case, no ! before "Name"
    Also can you try on a non default property, i.e. a property added to the doc type.
    It sounds like the expression tree is missing a couple of if statements when dealing with the lambda (this is something for me to fix, but I am trying to get some more accurate debugging info about what cases it fails with and which ones it works with)


  • Avatar558posts671karma
    Comment with ID: 84187
    Tom posted this reply July 21, 2011 @ 01:22

    Hi Gareth,

    unfortunately that does not work.. hmm.. if I try it on a non default property i get sequence contains no matching element as an exception.. but that looks like it's kicking it in to gear..

     

    is there a workaround for this kind of functionality I could use for now.. 

     

    In Extension Method Finder Line 130: firstMethod = methods.First(x => x.IsGenericMethodDefinition); that is where it is blowing up if I try on a non default


  • Avatar135posts301karma
    Comment with ID: 84188
    Gareth Evans posted this reply July 21, 2011 @ 01:26

    Okay, that's progress though, it suggests there's two bugs with ContainsAny

    The workaround would be to write a class library with your static extension method you want accessible with the reverse of ContainsAny (e.g. NotContainsAny), I think that was documented in part 3 as well, but referenced in part 5.

    I am not sure what's throwing the sequence contains no matching element.

    The other possible workaround would be a longhand where, for example:

    .Where("Name != "blah" && Name != "blah2") but you'd need to know the values

    Or just have a standard contains check as an if statement inside your loop (messy but a workaround always is)

    Gareth

     


  • Avatar135posts301karma
    Comment with ID: 84189
    Gareth Evans posted this reply July 21, 2011 @ 01:28

    Re:

    In Extension Method Finder Line 130: firstMethod = methods.First(x => x.IsGenericMethodDefinition); that is where it is blowing up if I try on a non default

    That suggests that "methods" didn't find any methods by that name.. odd.

    I'll change that First to be FirstOrDefault and null check for the 4.7.1 beta


  • Avatar558posts671karma
    Comment with ID: 84191
    Tom posted this reply July 21, 2011 @ 01:32

    Cheers Gareth,

    hmm might have to write an extension method and see how I go.. please keep me posted on this as it's a really awesome feature.. ill get one of the guys to have a look here with me this morning to see if we can track down what it's doing..

     

    I wonder if I could take one of your contains any methods from the source code and move it into an extension..


  • Tom posted this reply July 21, 2011 @ 02:20

    Hi Guys,

    Just thought I'd post a workaround for you

    At Gareth's suggestion I made an extension method.. with an overload.. so!

    public static DynamicNodeList NotContainsAny(this DynamicNodeList all, List<string> valuesForComparison)

    {

    var items = all.Items.Where(item => !valuesForComparison.Contains(item.Name));

    return new DynamicNodeList(items);

    }

     

    public static DynamicNodeList NotContainsAny(this DynamicNodeList all, string propertyName, List<string> valuesForComparison)

    {

    var items = all.Items

    .Where(

    item =>

    {

    var property = item.GetProperty(propertyName);

    return (property != null) && !valuesForComparison.Contains(property.Value);

    }

    );

     

    return new DynamicNodeList(items);

    }


  • Avatar135posts301karma
    Comment with ID: 84193
    Gareth Evans posted this reply July 21, 2011 @ 02:33

    Awesome Tom,

    By the way, does anyone else actually use this feature ;)

     

    Gareth


  • Avatar558posts671karma
    Comment with ID: 84194
    Tom posted this reply July 21, 2011 @ 02:34

    haha :)


  • Avatar135posts301karma
    Comment with ID: 84344
    Gareth Evans posted this reply July 22, 2011 @ 01:31

    Fixed in 4.7.1


  • Avatar558posts671karma
    Comment with ID: 84346
    Tom posted this reply July 22, 2011 @ 01:41

    Thanks that'd be great I'll try the binary.. but im running 4.7.0..

    Cheers,

    Tom


Pages:

Please login or Sign up To post replies