Collating in LINQ with multiple entity records

0

I have a LINQ query that retrieves the average of a list of Notes grouped by sources of risk.

 var mediaControles = await (from ac in _context.AvaliacaoControle
 join c in _context.Controle on ac.ControleID equals c.ControleID
 join nc in _context.NotaControle on ac.NotaControleID equals 
 nc.NotaControleID
 group ac by ac.FonteDeRiscoID into groupDate
 select new EventoImpactoVM()
 {
 fonteID = groupDate.Key,
 NotaControle = groupDate.Average(p => p.NotaControle.Nota)
 }).ToListAsync();

The query below retrieves a list of risk sources grouped by Risk Event and the calculated mean of the probability field (error location) Calculates the mean of the Note field in the Note field of the previous query.

var avgProb = await (from ms in _context.AvaliacaoFonte
join prob in _context.Probabilidade on ms.ProbabilidadeID equals 
prob.ProbabilidadeID

//join da consulta anterior
join m_c in mediaControles on ms.FonteDeRiscoID equals m_c.fonteID
group new { m_c, ms } by ms into groupDate

select new EventoImpactoVM()
{                                         
eventoID = groupDate.Key.EventoID,

// calculation of average probability notes, it does not work! returns an error

  

lambda_method (Closure, f__AnonymousType4)

NotaProbabilidade = groupDate.Average(a 
=> a.ms.Probabilidade.NotaProbabilidade),

//cálculo da média da média
MediaControle = groupDate.Average(a => a.m_c.NotaControle)
}).ToListAsync(); 

So I understand the error in question points to the instantiation of the anonymous variable ms, I've tried several alternatives but I can not solve it, I think the problem is in group new {m_c, ms}. When I group by only one of the variables m_c or ms the error does not occur, when I do the grouping by the two, only m_c returns the result. Thanks to all who can help.

    
asked by anonymous 08.05.2018 / 15:12

1 answer

0

Try to group only by specific fields (the fields that will be used in your select clause) instead of trying to group by the entire entity. Do the following:

group new 
{
    ms.EventoID, 
    ms.Probabilidade.NotaProbabilidade,
    m_c.NotaControle
} by ms into groupDate
    
03.07.2018 / 16:39