Search code examples
c#linq2sxc

2sxc Linq query for Categories


I have entities: Documents, Category, DocList

Documents and DocumentList can have multiple categories selected.

I want to make filter for Documents that are in one or more categories.

// all documents
var items = AsDynamic(App.Query["DocList"]["AllDocs"]);
// categories for filter
var fcat = Content.Category;

//linq query??
items = items.Where(d=>d.Category ....????....);

Can and how I can make this kind of filter?

Content.Category is list of Categories.

So I want to show list of items if there are in any of categories, not only one

something like this: linq where list contains any in list


Solution

  • Tested on: dnn 9.1.1 / 2sxc 9.14.0

    My final code:

    @using System
    @using ToSic.SexyContent
    @using System.Collections.Generic
    @using System.Linq
    @{
        var items = AsDynamic(App.Data["Document"]);
        var tags = Content.Tags;
        items = items.Where(i => (i.Tags as List<DynamicEntity>).Any(c => ((List<DynamicEntity>)tags).Any(f => c.EntityId == f.EntityId)));
    }
    <div class="sc-element">
        <h1>@Content.Title</h1>
        @Edit.Toolbar(Content)
    </div>
    @foreach(var t in items)
    {
        <div>@t.Title</div>
    }
    

    Document has field : Title (string) and Tags (Tag Entity / multiple)

    Content is "Document List" with field "Tags" (type of Tag / multiple)

    That way my code work.