あなたの列はnull許容です
int maxAge = context.Persons.Select(p => p.Age).Max() ?? 0;
あなたの列はnull許容ではありません
int maxAge = context.Persons.Select(p => p.Age).Cast<int?>().Max() ?? 0;
どちらの場合も、2番目のコードを使用できます。を使用するとDefaultIfEmpty
、サーバーでより大きなクエリを実行できます。興味のある人のために、EF6に相当するものは次のとおりです。
なしでクエリ DefaultIfEmpty
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
MAX([Extent1].[Age]) AS [A1]
FROM [dbo].[Persons] AS [Extent1]
) AS [GroupBy1]
でクエリ 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]