linq 如何通过键过滤链接查询?

axr492tv  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(150)

我想使用C#和EF Core过滤我的查询,以便从该列表中:

{
  "result": [
    {
      "commissionId": "b99a0152-b3a5-4ff6-f19e-08da6a45d751",
      "commissionCode": "0001",
      "commissionDescription": "Description1",
      "activityId": "323e6237-c3f6-4616-3117-08da6a28ad38",
      "activityCode": "01.1",
      "activityDescription": "DELETE FILE",
      "minuteWorked": 10440,
      "activityList": null,
      "timeWorked": "7.06:00:00"
    },
    {
      "commissionId": "b99a0152-b3a5-4ff6-f19e-08da6a45d751",
      "commissionCode": "0001",
      "commissionDescription": "Description1",
      "activityId": "95d37329-acac-4443-3118-08da6a28ad38",
      "activityCode": "01.2",
      "activityDescription": "DOWNLOAD FILE",
      "minuteWorked": 15,
      "activityList": null,
      "timeWorked": "00:15:00"
    },
    {
      "commissionId": "b99a0152-b3a5-4ff6-f19e-08da6a45d751",
      "commissionCode": "0001",
      "commissionDescription": "Description1",
      "activityId": "89fd1d93-b5b8-4c08-3119-08da6a28ad38",
      "activityCode": "01.3",
      "activityDescription": "FILE SAVE",
      "minuteWorked": 0,
      "activityList": null,
      "timeWorked": "00:00:00"
    },

我得到这个:

{
  "result": [
    {
      "commissionId": "b99a0152-b3a5-4ff6-f19e-08da6a45d751",
      "commissionCode": "0001",
      "commissionDescription": "Description1",
      "activityList": "{ 323e6237-c3f6-4616-3117-08da6a28ad38",95d37329-acac-4443-3118- 
                       08da6a28ad38,89fd1d93-b5b8-4c08-3119-08da6a28ad38 }
      "timeWorked": "7.06:00:00"
    },

刚才澄清的第一个单子是陈言得出的:

  • GroupBy( c => new {c.ActivityId, c.CommissionId}
  • 然后道:
Select(grp => new RegistrationStatisticViewModel() {CommissionId = grp.Key.CommissionId,
         CommissionCode = grp.First().Commission.Code,
         CommissionDescription = grp.First().Commission.Description,
         ActivityId = grp.Key.ActivityId,
         ActivityCode = grp.First().Activity.Code,
         ActivityDescription = grp.First().Activity.Description,
         MinuteWorked = grp.Sum(c => c.MinuteWorked)
         })
         .ToListAsync(),

第二个列表本质上将所有Commission与相同的id分组,然后我猜它有一个List<T>类型的属性,存储所有activityId
因此,我想将所有活动分组为一个commissionId,但只有一个commissionCode等。
感谢回复的人!

4szc88ey

4szc88ey1#

试试这个:

grp
            .ToList()
            .Where(cm => cm.CommissionId = CommissionId)
            .ToList()
            .ForEach(c =>
            {
                grp.activityList.Add(c.activityId);
            });

注:未测试代码。

相关问题