How to get the amount of multiple properties on a list?

Asked

Viewed 42 times

-1

I have a list that has over 10,000 Curriculum Grades. Within Curriculum Grade I have the name of the Discipline and the Teaching that it is

There are 15 subjects that can be repeated for each Teaching.

Ex:

Elementary School has the disciplines of mathematics, Portuguese and sciences.

High school has English, physics and biology, mathematics and portuguese.

I need to take the amount of each discipline that exists in each teaching and the sum of the same discipline in all teaching.

Does anyone have any simple way of getting that information? I thought I’d make a foreach for every discipline, but the code would be gigantic.

I have a Dto who owns:

public int AnoConcurso { get; set; } 
public List<RelatorioDisciplinasDto> ComponenetesCurriculares { get; set; } 

Within RelatorioDisciplinasDto:

public int CodigoNivelEnsino { get; set; } 
public string NivelEnsino { get; set; } 
public string Disciplina { get; set; } 

The problem is that in groupBy when I type grade.ComponenetesCurriculares and then I put .(dot) he can’t see Disciplina.

1 answer

0


By the statement I gave an example to show the answer and be clearer.

Existing class:

public class RelatorioDisciplinasDto{
  public int CodigoNivelEnsino { get; set; } 
  public string NivelEnsino { get; set; } 
  public string Disciplina { get; set; }
} 

Example on console:

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass {
  public static void Main (string[] args) {
    List<RelatorioDisciplinasDto> grades = new List<RelatorioDisciplinasDto>();

    //Ensino Médio possui inglês, física e biologia, matemática e português.
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 1, NivelEnsino = "Ensino Médio", Disciplina = "Inglês"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 1, NivelEnsino = "Ensino Médio", Disciplina = "Física"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 1, NivelEnsino = "Ensino Médio", Disciplina = "Biologia"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 1, NivelEnsino = "Ensino Médio", Disciplina = "Matemática"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 1, NivelEnsino = "Ensino Médio", Disciplina = "Português"
    });

    //Ensino Fundamental possui as disciplinas de matemática, português e ciências.
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 2, NivelEnsino = "Ensino Fundamental", Disciplina = "Matemática"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 2, NivelEnsino = "Ensino Fundamental", Disciplina = "Português"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 2, NivelEnsino = "Ensino Fundamental", Disciplina = "Ciências"
    });
    grades.Add(new RelatorioDisciplinasDto{
      CodigoNivelEnsino = 2, NivelEnsino = "Ensino Fundamental", Disciplina = "Ciências"
    });

    var quantidadeDeCadaDisciplinasPorEnsino = grades
      .GroupBy(
        grade => grade.NivelEnsino,
        grade => grade.Disciplina,
        (nivelEnsino, disciplinas) => new 
        {
          TipoDeEnsino = nivelEnsino,
          CountDisciplinas = disciplinas.Count(),
          Disciplinas = disciplinas
        }
      );

    Console.WriteLine("\nQuantidade de cada disciplina que exista em cada ensino:\n");

    foreach (var result in quantidadeDeCadaDisciplinasPorEnsino)
    {
        Console.WriteLine("Tipo de Ensino: " + result.TipoDeEnsino);
        Console.WriteLine("Número de Disciplinas: " + result.CountDisciplinas);
        var group = result.Disciplinas
          .GroupBy(s => s)
          .Select(g => new { Name = g.Key, Count = g.Count() });
        foreach (var disciplina in group){
            Console.WriteLine("Nome da Disciplina: " + disciplina.Name);
            Console.WriteLine("Quantidade: " + disciplina.Count);
          }
        Console.WriteLine();
    }

    Console.WriteLine("\n----------------------------\n");
    Console.WriteLine("Somatório das disciplinas em todos os ensinos:\n");

    var somatorioDeCadaDisciplina = grades
      .Select(grade => grade.Disciplina)
      .GroupBy(s => s)
      .Select(g => new { Name = g.Key, Count = g.Count() });

    foreach (var result in somatorioDeCadaDisciplina)
    {
      Console.WriteLine("Nome da Disciplina: " + result.Name);
      Console.WriteLine("Quantidade: " + result.Count);
    }
  }
}
  • I have a Dto that has: public int Anoconcurso { get; set; } public List<Reportdisciplinesdto> Componenetescurricular { get; set; Inside Reportdisciplinesdto: public int Codigonivelensino { get; set; } public string Nivelensino { get; set; } public string Disciplina { get; set; } The problem is that in groupBy when I type in the grid. (dot), he does not see Discipline. . Groupby( grid => grid.Componenetescurricular,

  • My statistical report is coming to the service with the information I want. public int Anoconcurso { get; set; } public List<Reportdisciplinesdto> Componenetescurricular { get; set; } Dái this list comes from the discipline name Nivelensino. But when I put it on Groupby, I can’t access discipline by calling it a grid. In theory it would be grid.ComponenetesCurriculars.Discipline.

  • It was, a thousand apologies. =(

  • Thank you very much. I can’t thank you enough.

  • I really appreciate it. I’m sorry I didn’t get the full information.

  • @Thunanfreitaschiu if I helped to get the answer put the answer as accepted

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.