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

2
  • What is the type of fcat? is it a collection? Commented Apr 17, 2017 at 9:25
  • Category is DynamicEntity Commented Apr 17, 2017 at 9:52

2 Answers 2

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(...)
Sign up to request clarification or add additional context in comments.

7 Comments

I make mistake before and This don't work, becouse fcat = list of dynamic entities.
I made a correction which should cover your case - did it help?
sorry for late response. I try your update with variations but stuck with message: Error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot convert type 'System.Collections.Generic.List<ToSic.SexyContent.DynamicEntity>' to 'System.Collections.Generic.List<object>
Did you use List<dynamic> or List<object>? You should really use List<dynamic>
|
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.

1 Comment

Perfect. BTW: You probably just need the first as List<...>, the second one c => ((List...) is probably not necessary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.