Sto riscontrando questo errore. Vedo che la ragione è perché la media restituita a volte è 0.00 che dal punto di vista dei dati è accurata. Questa query SQL funziona correttamente, ma è perché inserisce 0.00 automaticamente.La sequenza non contiene elementi - LINQ, MVC, Media
LINQ si lamenta e così ho provato a utilizzare DefaultIfEmpty() ma dice che si aspetta il mio ViewModel.
Dim ticketCounts = From t In queue _
Where _
(t.StatusId = 2) And _
(t.CloseDate.Year = Convert.ToDateTime(DateTime.Now).Year) And _
(t.ResolutionDays > 0)
Group t By _
Column1 = CType(t.CloseDate.Month, Integer), _
Column2 = CType(t.CloseDate.ToString("MMMM"), String) _
Into g = Group _
Order By Column1 _
Select _
Id = Column1, _
Month = Column2, _
Critical = g.Where(Function(t) t.PriorityId = 1).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
High = g.Where(Function(t) t.PriorityId = 2).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
Normal = g.Where(Function(t) t.PriorityId = 3).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
Low = g.Where(Function(t) t.PriorityId = 4).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
Total = g.Where(Function(t) t.Id <> Nothing).DefaultIfEmpty().Average(Function(t) t.ResolutionDays)
AGGIORNATO! Questa è la query SQL che fa la stessa cosa che mi serve VB.
SELECT
DATENAME(MONTH,t.CloseDate) AS 'Month',
AVG(CASE WHEN (t.PriorityId = 1) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Critical',
AVG(CASE WHEN (t.PriorityId = 2) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'High',
AVG(CASE WHEN (t.PriorityId = 3) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Normal',
AVG(CASE WHEN (t.PriorityId = 4) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Low',
AVG(CAST(t.ResolutionDays AS Decimal(18, 2))) AS 'Monthly Average'
FROM
tblMaintenanceTicket t
WHERE
t.StatusId = 2
AND YEAR(t.CloseDate) = year(getdate())
GROUP BY
MONTH(t.CloseDate),
DATENAME(MONTH,t.CloseDate)
ORDER BY
MONTH(t.CloseDate)
ho provato quel suggerimento di un paio di modi diversi, ma può questo tipo di cose essere completa in linea? Come un condizionale in linea? So chiaramente molto poco di LINQ lol. – Chuck
@Chuck perché non puoi usare defaultifempty? È pensato per questo genere di cose – nawfal
Non ero sicuro di dove metterlo. Se lo metto subito dopo la clausola where come g.Where (Function (t) t.PriorityId = 4) .DefaultIfEmpty() che non può convertire in ViewModelType. – Chuck