想要通过 2scx 模板中的子实体进行过滤
Want to filter by a child Entity in 2scx template
我有一个名为 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 但这给出了一个错误 "Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List' and 'int' a" - 我假设因为 YearGiven 是一个实体,所以实际上是一个 List<>。
所以然后我改到代码中的下一个foreach并得到这个错误:-
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type."
我找不到任何模板示例可以完成我想做的事情,但我所做的一切都不起作用。
N.B。我现在已经在其中硬编码了“2017”以保持简单,但显然它会在外部循环中找到每年。
这里有一个具有类似架构的简单示例,如果您想对其进行调整。我基本上使用变量 (currCat) 来跟踪和处理 'on-change of category'。希望您可以忽略所有 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>
我有一个名为 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 但这给出了一个错误 "Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List' and 'int' a" - 我假设因为 YearGiven 是一个实体,所以实际上是一个 List<>。
所以然后我改到代码中的下一个foreach并得到这个错误:-
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type."
我找不到任何模板示例可以完成我想做的事情,但我所做的一切都不起作用。
N.B。我现在已经在其中硬编码了“2017”以保持简单,但显然它会在外部循环中找到每年。
这里有一个具有类似架构的简单示例,如果您想对其进行调整。我基本上使用变量 (currCat) 来跟踪和处理 'on-change of category'。希望您可以忽略所有 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>