Home Ask Login Register

Developers Planet

Your answer is one click away!

CerIs February 2016

Linq spit numeric list in search (kentico)

In kentico the standard way to get documents in below (which I believe is based on ObjectQuery and has linq commands). Im trying to filter it by one more field "newsCategory" which contains data like "1|2|3". So I cant add .Search("newsCategory", 1) etc because I need to split the list before I can search it. What direction should I be looking? A select sub-query? (Im new to linq)

// Get documents
var news = DocumentHelper.GetDocuments("CMS.News")
.OnSite("CorporateSite")
.Path("/News", PathTypeEnum.Children)
.Culture("en-us")
.CombineWithDefaultCulture(false);         

Answers


josh February 2016

I believe something like this would work. There is a wherein property that should be able to pull the value out. Not exactly sure how it would handle the scenario of having a 1 and then an 11, but it may be work looking into.

// Get documents
var news = DocumentHelper.GetDocuments("CMS.News")
.OnSite("CorporateSite")
.Path("/News", PathTypeEnum.Children)
.Culture("en-us")
.CombineWithDefaultCulture(false)
.WhereIn("NewsCategory",1);


Zach Perry February 2016

Are you sure your data is 1|2|3 and not 1|2|3| or |1|2|3 ?

If it is, you could do .Where("NewsCategory", QueryOperator.Like, "%" + id + "|%")

Otherwise you may have to get back more results, and then loop through them and split the values to find the exact one you want.


Jerreck February 2016

EDIT: Check out this article that shows some more advanced where commands you can use with the Data Query API. You should be able to MacGyver a proper filter with those options.

I believe you're looking for:

.WhereLike("DocumentCategoryID", "CategoryID");
//OR
.WhereLike("DocumentCategory","CategoryName");

I don't have v8 installed to double check which exact key/value pair to filter by, but according to this Document Query API article you filter document sets with the WhereLike() method.

According to the API documentation, GetDocuments() returns a MultiDocumentQuery object. I'm not 100% certain if that implements IEnumerable, so you may not even be able to use LINQ with it.


Roman Hutnyk February 2016

As far as this is a field from the coupled table, you can't access it through property, but have to use GetValue() instead. Once you've got, you can work with it like with regular string:

var news = DocumentHelper.GetDocuments("CMS.News")
            .OnSite("CorporateSite")
            .Path("/News", PathTypeEnum.Children)
            .Culture("en-us")
            .CombineWithDefaultCulture(false)
            .Where(d => d.GetStringValue("newsCategory","").Split('|').Contains("1"));  

Post Status

Asked in February 2016
Viewed 3,227 times
Voted 6
Answered 4 times

Search




Leave an answer


Quote of the day: live life