1

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

Community
  • 1
  • 1
Jernej Pirc
  • 504
  • 1
  • 4
  • 13

2 Answers2

1

So parts of this are explained in the wiki https://github.com/2sic/2sxc/wiki/DotNet-Query-Linq

but I must admit that there is no example for exactly your question. I believe you want something like:

// filter - keep only those that have this Category
// note that the compare must run on the EntityId because of object wrapping/unwrapping
    items = items.Where(i => 
        (i.Category as List<dynamic>).Any(c => c.EntityId == fcat.EntityId))

So this should work :)

Additional solution if fcat is a list should be approx. like this

// filter - keep only those that have this Category
// note that the compare must run on the EntityId because of object wrapping/unwrapping
    items = items.Where(i => 
        (i.Category as List<dynamic>).Any(c => fcat.Any(f => c.EntityId == f.EntityId)))

If this causes an error, you'll probably need to cast fcat into something like

((List<dynamic>)fcat).Any(...)
iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
1

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.

Jernej Pirc
  • 504
  • 1
  • 4
  • 13
  • Perfect. BTW: You probably just need the first `as List<...>`, the second one `c => ((List...)` is probably not necessary – iJungleBoy Feb 13 '18 at 15:46