2sxc Linq 查询类别

2sxc Linq query for Categories

我有实体:文档、类别、文档列表

Documents 和 DocumentList 可以选择多个类别。

我想对属于一个或多个类别的文档进行筛选。

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

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

可以制作这种滤镜吗?

Content.Category 是类别列表。

所以我想显示任何类别中的项目列表,而不仅仅是一个

像这样:linq where list contains any in list

所以部分内容在 wiki 中有解释 https://github.com/2sic/2sxc/wiki/DotNet-Query-Linq

但我必须承认,您的问题没有确切的例子。我相信你想要这样的东西:

// 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))

所以这应该有效:)

如果 fcat 是一个列表,额外的解决方案应该是大约。像这样

// 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)))

如果这会导致错误,您可能需要将 fcat 转换为类似

((List<dynamic>)fcat).Any(...)

测试于:dnn 9.1.1 / 2sxc 9.14.0

我的最终代码:

@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 List",字段 "Tags"(标记类型/多个)

这样我的代码就可以工作了。