You are developing an application that uses the Microsoft ADO.NET Entity Framework to retrieve order
information from a Microsoft SQL Server database. The application includes the following code. (Line
numbers are included for reference only.)
The application must meet the following requirements:
-Return only orders that have an OrderDate value other than null.
-Return only orders that were placed in the year specified in the OrderDate property or in a later year.
-Not raise an exeption
You need to ensure that the application meets the requirements.
Which code segment should you insert at line 08?
A.
Where order.OrderDate.Value != null && order.OrderDate.Value.Year > = year
B.
Where order.OrderDate.Value = = null && order.OrderDate.Value.Year = = year
C.
Where order.OrderDate.HasValue && order.OrderDate.Value.Year > = year
D.
Where order.OrderDate.Value.Year = = year
Explanation:
qloslaw: in previous version correct answer was A because C was like that: Where order.OrderDate.Value!=
null && order.OrderDate.Value.Year = = year //diffrent operator
A.
Where order.OrderDate.Value != null && order.OrderDate.Value.Year > = year
C.
Where order.OrderDate.HasValue && order.OrderDate.Value.Year > = year
whats is the difference between these two queries, can anybody explain?
the OrderDate is defined as a Nullable type (first line), therefor the right answer uses the HasValue property as defined by the Nullable type.
yep, “order.OrderDate.Value != null” will throw exception if OrderDate is null. Correct would be “order.OrderDate != null”.
A
C
C
C
Must be C as OrderDate is Nullable (using ?), so we have to use .HasValue to avoid raising an exception in case OrderDate is null and we are trying to access its value => OrderDate.Value