nullable object must have a value

前端 未结 8 1749
梦如初夏
梦如初夏 2020-11-30 00:17

There is paradox in the exception description: Nullable object must have a value (?!)

This is the problem:

I have a DateTimeExtended class, tha

8条回答
  •  伪装坚强ぢ
    2020-11-30 00:39

    When using LINQ extension methods (e.g. Select, Where), the lambda function might be converted to SQL that might not behave identically to your C# code. For instance, C#'s short-circuit evaluated && and || are converted to SQL's eager AND and OR. This can cause problems when you're checking for null in your lambda.

    Example:

    MyEnum? type = null;
    Entities.Table.Where(a => type == null || 
        a.type == (int)type).ToArray();  // Exception: Nullable object must have a value
    

提交回复
热议问题