2017-04-17 72 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 –