想要通过2scx模板中的子实体进行过滤

时间:2018-04-17 05:07:35

标签: dotnetnuke 2sxc

我有一个名为Awards的列表实体,其名称(字符串)和YearGiven(实体)作为其字段。

我想显示按年份分组的所有奖项。 即

2017
---鲍勃
---苏
2016
---弗雷德
2015年

这是我的模板:

@using ToSic.SexyContent
@functions
{
    // variable which will contain the sorted categories
    IEnumerable<dynamic> sortedCategories;


    // Prepare the data - get all categories through the pipeline
    public override void CustomizeData()
    {
        // get all categories of these questions, then get the distinct entities 
        // this could all be done on 1 line, but it would be harder for people who don't know LINQ yet
        var awardsInThisModule = AsDynamic(App.Data["Awards"].List);
        var categoriesUsed = awardsInThisModule.SelectMany(q => ((List<DynamicEntity>)q.YearGiven));

        var distinctCategories = categoriesUsed.Select(AsEntity).Distinct();    // Distinct only works reliably when cast as entity
        sortedCategories = AsDynamic(distinctCategories).OrderBy(q => q.Year);

    }

}
<link rel="stylesheet" href="@App.Path/assets/awards.css" data-enableoptimizations="true" />

@foreach (var cat in sortedCategories)
        {
           <h3> @cat.Year</h3>
             foreach (var q in AsDynamic(App.Data["Awards"].List).Where(t => t.Name == "Bob").OrderBy(q => q.Name))
            {
                //this works fine and puts Bob against each year


                <h2>@q.Name</h2>

            }

           foreach (var q in AsDynamic(App.Data["Awards"].List).Where(t => t.Year.Select(a => AsDynamic(a).Year) == "2017"))
            {
                //this is what I actually want to do and fails
                <h2>@q.Name</h2>

            }



             <br />
        }

我首先将Where子句更改为t.YearGiven == 2016,但这会产生错误&#34;运算符&#39; ==&#39;不能应用于类型&#39; System.Collections.Generic.List&#39;的操作数。和&#39; int&#39;一个&#34; - 我假设因为YearGiven是一个实体,所以实际上是一个List&lt;&gt;。
所以我在代码中改为下一个foreach并得到了这个错误: - &#34;不能将lambda表达式用作动态调度操作的参数,而无需先将其转换为委托或表达式树类型。&#34;

我无法找到任何模板示例,它可以完成我尝试做的事情而且无法完成任何工作。

N.B。我已经硬编码2017年&#39;在那里暂时保持简单,但显然会在外循环中找到每一年。

1 个答案:

答案 0 :(得分:0)

如果您想要调整它,这是一个具有类似架构的简单示例。我基本上使用变量(currCat)来跟踪和处理“类别的变化”。希望你可以忽略所有的expando / collapse东西。这是最终的结果: http://www.blackandco.com/Vendor-Linecard

<div id="vendor-list" role="tablist" class="small">
    @{
        int currCat = 0;
        int firstCo = 851; // Abrasives
        foreach (var aCat in AsDynamic(App.Data["CompanyCategories"])
                .Where(c => c.CategoryActiveYN == true)
                .OrderBy(c => c.CategoryName) 
                )
        {
            currCat = aCat.EntityId;
            <div class="card">
                <div class="card-header" role="tab" id="@string.Format("{0}{1}", "heading", @currCat)">
                    <h5 class="mb-0@((currCat == firstCo) ? "" : " collapsed")" data-toggle="collapse" href="@string.Format("{0}{1}", "#collapse", @currCat)" 
                        aria-expanded="@((currCat == firstCo) ? "true" : "false")" aria-controls="@string.Format("{0}{1}", "collapse", @currCat)">
                        @aCat.CategoryName
                    </h5>
                </div>
                <div id="@string.Format("{0}{1}", "collapse", @currCat)" class="collapse@((currCat==firstCo) ? " show" : "")" role="tabpanel" aria-labelledby="@string.Format("{0}{1}", "heading", @currCat)" data-parent="#accordion" aria-expanded="@((currCat==firstCo) ? "true" : "false")">
                    <div class="card-body">
                        <ul>
                            @foreach (var vComp in AsDynamic(App.Data["Company"])
                        .Where(v => v.CompanyActiveYN && v.IncludeOnVendorCards)
                        .OrderBy(v => v.CompanyName)
                        )
                            {
                                foreach (var vCat in vComp.CompanyCategory)
                                {
                                    if (vCat.EntityId == currCat)
                                    {
                                        <li>@vComp.CompanyName<span></li>
                    }
                }
                            }
                        </ul>
                    </div>
                </div>
            </div>
        }
    }
</div>