Home > Net >  Ho to filter on linq queryes c# by key?
Ho to filter on linq queryes c# by key?

Time:08-12

i want to filter my query using c# and EF Core so that from this list:

{
  "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"
    },

I get this:

{
  "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"
    },

Just to clarify the first list is obtained doing a:

  • GroupBy( c => new {c.ActivityId, c.CommissionId}
  • And then:
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(),

The second list essentially groups all Commission with the same id and then i guess ? it has a property of type List<T> that stores all the activityId.

So i want to group all the activity for one commissionId but only have one commissionCode etc.

Thanks to whoever replies!

CodePudding user response:

Try this:

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

NOTE: untested code.

  • Related