Search In
Learn from 350 other Umbracians at the annual Umbraco Conference - CodeGarden '13. More than twenty high quality sessions, open spaces, hackathons and social events you'll remember. Not to be missed! Less than 25 tickets left - get yours now!
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<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); } }
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); } }
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)
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
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
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
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..
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);
);
Awesome Tom,
By the way, does anyone else actually use this feature ;)
haha :)
Fixed in 4.7.1
Thanks that'd be great I'll try the binary.. but im running 4.7.0..
Cheers,
Tom