How to apply Where condition on Umbraco Collection
Asked Answered
H

2

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

Haberdasher answered 7/9, 2016 at 10:31 Comment(0)
P
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.

Papst answered 26/9, 2016 at 1:42 Comment(1)
Although I could not found exact solution, I think you both are conceptually right.Haberdasher
B
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!=\"\"")
Busby answered 7/9, 2016 at 14:39 Comment(6)
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.Antigorite
Error : Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree typeHaberdasher
Try Umbraco.TypedContent instead of Umbraco.ContentBusby
var workCollection = Umbraco.TypedContent(workList); I made changes like this, still getting same errorHaberdasher
@MarcinZajkowski : I checked your answer and I am getting same error which I mentioned above. I am trying this in cshtml. Does it matter?Haberdasher
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.Papst
P
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.

Papst answered 26/9, 2016 at 1:42 Comment(1)
Although I could not found exact solution, I think you both are conceptually right.Haberdasher

© 2022 - 2024 — McMap. All rights reserved.