6

I want to apply where condition on Umbraco Collection.

Code:

var workList = CurrentPage.work.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var workCollection = Umbraco.Content(workList);
@foreach (var item in workCollection.Where("productImage!=\"\"").Skip((i - 1) * iterationCount).Take(iterationCount))

But I always get data without filter.
ProductImage is media picker enter image description here enter image description here

Ubiquitous Developers
  • 3,637
  • 6
  • 33
  • 78

2 Answers2

1

So I guess what you want to do is get items from workcollection that have a filled projectImage property?

I personally like to do this with a lambda expression, in your case it would be something like this

workCollection.Where(x => x.HasValue("productImage"))

instead of

workCollection.Where("productImage!=\"\"")
Mark
  • 3,231
  • 3
  • 32
  • 57
  • i even tend to go one futher and also do: workCollection.Where(x => x.HasProperty("productImage") && x.HasValue("productImage")) just to be sure as sometimes if you have just added a new property it might not be available until the indexes have been rebuilt, more like a belt and braces approach. – denford mutseriwa Sep 08 '16 at 00:22
  • Error : Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type – Ubiquitous Developers Sep 08 '16 at 06:01
  • Try Umbraco.TypedContent instead of Umbraco.Content – Mark Sep 09 '16 at 07:36
  • var workCollection = Umbraco.TypedContent(workList); I made changes like this, still getting same error – Ubiquitous Developers Sep 12 '16 at 06:45
  • @MarcinZajkowski : I checked your answer and I am getting same error which I mentioned above. I am trying this in cshtml. Does it matter? – Ubiquitous Developers Sep 26 '16 at 10:38
  • It's only for cshtml, so it's not the case. Did you change "var" to "IPublishedContent" in foreach loop? I tested it and it's working like a charm. – Marcin Zajkowski Sep 26 '16 at 10:42
1

If you want to stick to dynamic object, you should try:

var workList = CurrentPage.work.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var workCollection = Umbraco.Content(workList);
@foreach (var item in workCollection.Where("productImage != null && productImage != string.Empty").Skip((i - 1) * iterationCount).Take(iterationCount)) { ... }

Personally, I prefer to deal with strongly typed objects, so another solution may be:

var workList = CurrentPage.work.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var workCollection = Umbraco.TypedContent(workList);
@foreach (IPublishedContent item in workCollection.Where(x => x.HasValue("productImage")).Skip((i - 1) * iterationCount).Take(iterationCount)) { ... }

For more informations check: https://our.umbraco.org/documentation/reference/templating/mvc/querying.

You can also check a package called Umbraco Core Property Value Converters: https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/ which is automatically converting some data type values into easily accessed objects / lists etc. E.g. media picker value is returned as IPublishedContent model and you can access it's properties directly from the returned value.

Marcin Zajkowski
  • 1,668
  • 10
  • 14