Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 1.93 KB

handle-null-values-in-query-expressions.md

File metadata and controls

36 lines (27 loc) · 1.93 KB
title description keywords author manager ms.author ms.date ms.topic ms.prod ms.technology ms.devlang ms.assetid
Handle null values in query expressions
How to handle null values in query expressions.
.NET, .NET Core, C#
stevehoag
wpickett
wiwagn
12/1/2016
article
.net-core
.net-core-technologies
dotnet
ac63ae8b-724d-4251-9334-528f4e884ae7

Handle null values in query expressions

This example shows how to handle possible null values in source collections. An object collection such as an xref:System.Collections.Generic.IEnumerable%601 can contain elements whose value is null. If a source collection is null or contains an element whose value is null, and your query does not handle null values, a xref:System.NullReferenceException will be thrown when you execute the query.

Example

You can code defensively to avoid a null reference exception as shown in the following example:

[!code-cscsProgGuideLINQ#82]

In the previous example, the where clause filters out all null elements in the categories sequence. This technique is independent of the null check in the join clause. The conditional expression with null in this example works because Products.CategoryID is of type int? which is shorthand for Nullable<int>.

Example

In a join clause, if only one of the comparison keys is a nullable value type, you can cast the other to a nullable type in the query expression. In the following example, assume that EmployeeID is a column that contains values of type int?:

[!code-cscsProgGuideLINQ#83]

See also

xref:System.Nullable%601
LINQ query expressions
Nullable types