【发布时间】:2018-01-08 14:01:32
【问题描述】:
我有一个表格,它为每个“系统类型”收集两个总数,可从下拉列表中选择(出于安全目的,我不得不混淆我的场景的细节,抱歉。)
用户可以根据需要添加任意数量的项目。每个都有一个 AM 计数和一个 PM 计数。
架构无法更改。
我需要用 LINQ 编写的 CRUD 操作。我正在努力想出“读取”语法,只是为了将数据从表中获取到表单中。需要限制在 ID 5 及以上的 DySystems。
如果没有 AM/PM 方面,并且每个 DySystem 只有一个数量,我可以使用我编写的以下代码:
public List<DySystemCountDto> GetDySystemCounts(int surveyId)
{
using (_context)
{
var joinData = _context.DySystemResourceCounts.Where(r => r.ResourceCount.SurveyId == surveyId)
.Include(r => r.ResourceCount)
.ToList();
var existingCounts = joinData.Select(r => new DySystemCountDto
{
ResourceCountId = r.ResourceCountId,
Quantity = r.ResourceCount.Quantity,
DySystemId = r.DySystemId
}).ToList();
return existingCounts;
}
}
要在视图中使用它:
<tr>
<th>DySystem</th>
<th>Count</th>
<th></th>
</tr>
@{
for (var i = 0; i < Model.DySystemCounts.Count; i++)
{
var item = Model.DySystemCounts[i];
<tr>
<td>
<select class="form-control" asp-items="Model.DySystems"
asp-for="@Model.DySystemCounts[i].DySystemId"></select>
<td>
<input type="hidden" asp-for="@Model.DySystemCounts[i].ResourceCountId" />
<input class="form-control" asp-for="@Model.DySystemCounts[i].Quantity"/>
</td>
<td>
<a href="#" class="remove">Remove</a>
</td>
</tr>
}
}
但是,由于 AM/PM 混合在一起,我不知道如何处理它。
类似于.. 对于每个 DySystem 的每个 Am 计数,还选择具有相同 DySystemId 的 Pm 计数。
我正在使用 .Net Core API 项目
【问题讨论】:
标签: c# .net entity-framework linq asp.net-core