Twoja kolumna ma wartość null
int maxAge = context.Persons.Select(p => p.Age).Max() ?? 0;
Twoja kolumna nie dopuszcza wartości null
int maxAge = context.Persons.Select(p => p.Age).Cast<int?>().Max() ?? 0;
W obu przypadkach możesz użyć drugiego kodu. Jeśli używasz DefaultIfEmpty
, wykonasz większe zapytanie na swoim serwerze. Dla zainteresowanych, oto odpowiednik EF6:
Zapytanie bez DefaultIfEmpty
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
MAX([Extent1].[Age]) AS [A1]
FROM [dbo].[Persons] AS [Extent1]
) AS [GroupBy1]
Zapytanie z DefaultIfEmpty
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
MAX([Join1].[A1]) AS [A1]
FROM ( SELECT
CASE WHEN ([Project1].[C1] IS NULL) THEN 0 ELSE [Project1].[Age] END AS [A1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN (SELECT
[Extent1].[Age] AS [Age],
cast(1 as tinyint) AS [C1]
FROM [dbo].[Persons] AS [Extent1]) AS [Project1] ON 1 = 1
) AS [Join1]
) AS [GroupBy1]