Querying post fields in Piranha

36 views Asked by At

I'm trying to work out how best to filter Posts by their Fields/Regions in Piranha without loading them all and then performing the filtering.

At the moment I have the following:

var posts = _db.Posts
            .Where(p => p.Published >= DateTime.Now)
            .Where(x => x.title == "somestring")
            .Select(p => p.Id)
            .ToList();

If I wanted to query a field in regions named FooArticleDetails how would I modify the above to achieve this?

I can retrieve the results by loading all the Posts and their related Regions but this is not preferred for obvious reasons.

loadedItems.Posts
    .Where(post =>
        suppliedDiff.Contains(
            "hard",
            post.Regions.FooArticleDetails.difficulty.Value)).ToLower()
        ))
    .ToList();
1

There are 1 answers

0
atoms On

I've figured it out. Post Regions are stored as PostFields. These can be accessed via the Fields property.

posts.Where(post =>
    post.Fields
        .Where(x =>
            x.FieldId == "difficulty" &&
            x.RegionId == "FooArticleDetails" &&
            suppliedDiff.Contains(x.Value.ToLower())
        )
        .Any());