Diksha Agrawal
Diksha Agrawal

Reputation: 1

Getting IGrouping error while creating a list using linq query

I want to create a list in C# , which looks like:

[
      {
         "Total": "11",
        "DocumentTypeName": "MMT",
        "ProceesedDocumentCount": "5",
        "UnProceesedDocumentCount": "6"
      },
      {

        "Total": "11",
        "DocumentTypeName": "CIMIC",
        "ProceesedDocumentCount": "3",
        "UnProceesedDocumentCount": "8"
      },
      {
          "Total": "13,
         "DocumentTypeName": "CROWN",
        "ProceesedDocumentCount": "7",
        "UnProceesedDocumentCount": "6"
      }
     ]

I have tried this code so far:

public async Task<ListResultDto<DashboardCountDto>> CountAllTrainDocuments()
            {
                    //var retList = new List<ListOfDocumentTypes>();
                  var allTrainFiles = await _trainDocumentRepository.GetAllListAsync();
                   var CountTrainFiles = allTrainFiles.GroupBy(t=> t.DocumentTypeName).
                                        Select(e => new DashboardCountDto{ 
                                             Total = e.Count(),
                                        DocumentTypeName = e.Key,
                 ProceesedDocumentCount = e.key.Count(g => g.Processed = true),
                 UnProceesedDocumentCount = e.key.Count(g => g.Processed = false),
                
             }).ToList();
              // return retList;
              
               CountTrainFiles.ForEach(
                 row => Console.WriteLine($"DocType: {row.DocumentTypeName}, ProcessedDocument: {row.ProceesedDocumentCount}, UnProcessedDocument: {row.UnProceesedDocumentCount}"));
    
                return new ListResultDto<DashboardCountDto>(CountTrainFiles);
    }

I am getting one error: IGrouping<string, TrainDocument>' does not contain a definition for 'key' and no accessible extension method 'key' accepting a first argument of type 'IGrouping<string, TrainDocument>' could be found (are you missing a using directive or an assembly reference?)

My DTO look like:

public class DashboardCountDto : EntityDto<long>
    {
        
        public int Total { get; set; }
        public string DocumentTypeName { get; set; }
        public int ProceesedDocumentCount { get; set; }
        public int UnProceesedDocumentCount { get; set; }
       

    }

Upvotes: 0

Views: 179

Answers (1)

Guru Stron
Guru Stron

Reputation: 143243

Try removing key before Count as you do for DashboardCountDto.Total (assuming TrainDocument has Processed property):

.Select(e => new DashboardCountDto
{ 
    Total = e.Count(),
    DocumentTypeName = e.Key,
    ProceesedDocumentCount = e.Count(g => g.Processed = true),
    UnProceesedDocumentCount = e.Count(g => g.Processed = false)       
}

Upvotes: 1

Related Questions