You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core API中基于LINQ实现DySystem的AM/PM数据查询

解决方案:适配AM/PM区分的Read逻辑与表单展示

看起来你需要把同一个DySystem对应的AM和PM计数合并到一个表单条目中,同时筛选出ID≥5的DySystem。下面是基于你现有代码的调整方案:

1. 调整DTO结构

首先修改DySystemCountDto,新增AM/PM对应的数量和资源ID字段,用来承载拆分后的数据:

public class DySystemCountDto
{
    public int DySystemId { get; set; }
    // 存储AM/PM对应的ResourceCount主键,用于后续更新操作
    public int? AmResourceCountId { get; set; }
    public int? PmResourceCountId { get; set; }
    // 对应AM/PM的计数值
    public int AmQuantity { get; set; }
    public int PmQuantity { get; set; }
}

2. 修改LINQ查询逻辑

通过分组查询,将同一个DySystemId下的AM/PM记录合并到一个DTO对象中,同时加入DySystemId≥5的筛选条件:

public List<DySystemCountDto> GetDySystemCounts(int surveyId) 
{ 
    using (_context) { 
        // 先筛选符合条件的记录:匹配surveyId,且DySystemId≥5,同时预加载ResourceCount数据
        var rawRecords = _context.DySystemResourceCounts
            .Where(r => r.ResourceCount.SurveyId == surveyId && r.DySystemId >= 5)
            .Include(r => r.ResourceCount)
            .ToList();

        // 按DySystemId分组,合并AM/PM数据
        var mergedCounts = rawRecords
            .GroupBy(record => record.DySystemId)
            .Select(group => new DySystemCountDto
            {
                DySystemId = group.Key,
                // 提取AM对应的记录(假设ResourceCount中有Period字段区分AM/PM,需替换为你实际的字段)
                AmResourceCountId = group.FirstOrDefault(r => r.ResourceCount.Period == "AM")?.ResourceCountId,
                AmQuantity = group.FirstOrDefault(r => r.ResourceCount.Period == "AM")?.ResourceCount.Quantity ?? 0,
                // 提取PM对应的记录
                PmResourceCountId = group.FirstOrDefault(r => r.ResourceCount.Period == "PM")?.ResourceCountId,
                PmQuantity = group.FirstOrDefault(r => r.ResourceCount.Period == "PM")?.ResourceCount.Quantity ?? 0
            })
            .ToList();

        return mergedCounts; 
    } 
}

注意:请将r.ResourceCount.Period替换为你数据库中实际区分AM/PM的字段(比如如果标识字段在DySystemResourceCounts表中,就改成r.Period)。如果某个DySystem只有AM或PM记录,代码会用0填充缺失的计数值,你也可以把AmQuantity/PmQuantity改成int?允许null。

3. 更新视图代码

调整表单布局,每个DySystem条目新增AM和PM两个输入框:

<tr> 
    <th>DySystem</th> 
    <th>AM Count</th>
    <th>PM 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>
        <td> 
            <input type="hidden" asp-for="@Model.DySystemCounts[i].AmResourceCountId" /> 
            <input class="form-control" asp-for="@Model.DySystemCounts[i].AmQuantity"/> 
        </td>
        <td> 
            <input type="hidden" asp-for="@Model.DySystemCounts[i].PmResourceCountId" /> 
            <input class="form-control" asp-for="@Model.DySystemCounts[i].PmQuantity"/> 
        </td>
        <td> 
            <a href="#" class="remove">Remove</a> 
        </td> 
    </tr> 
} }

额外提示:如果你的Model.DySystems下拉列表也需要只显示ID≥5的选项,记得在获取该列表时添加筛选条件:_context.DySystems.Where(d => d.Id >=5).Select(d => new SelectListItem { Text = d.Name, Value = d.Id.ToString() })。

关键注意事项

  • 确保数据库中存在区分AM/PM的标识字段,否则无法正确拆分数据;
  • 由于使用了using(_context),必须用Include预加载所有需要的关联数据,避免上下文释放后出现懒加载异常;
  • 后续的Create/Update逻辑也需要适配这个DTO,比如当提交表单时,需要为AM和PM分别创建/更新对应的ResourceCount和DySystemResourceCounts记录。

内容的提问来源于stack exchange,提问作者egmfrs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:46:31