2017-04-17 57 views
1

我有实体:Documents,Category,DocList2sxc Linq查询分类

Documents and 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

+0

'fcat'的类型是什么?它是一个集合? –

+0

类别是DynamicEntity –

回答

1

测试上: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> 
} 

文件有场:标题(字符串)和标签(标签实体/多个)

内容是“文件列表”与现场“标签”(类型标签/倍数)

这样,我的代码工作。

+0

完美。顺便说一句:你可能只需要第一个'列表<...>',第二个'c =>((List ...)'可能没有必要 – iJungleBoy

1

因此这部分在维基解释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(...) 
+0

我犯错之前,这不起作用,becouse fcat =动态实体列表。 –

+0

我对问题进行了更正。 –

+0

我需要像这样的东西:http://stackoverflow.com/questions/10667675/linq-where-list-contains-any-in-list –