Which code segment should you insert at line 08?

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.
You need to ensure that the application meets the requirements.
Which code segment should you insert at line 08?

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.
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:
*For the requirement to use an OrderDate value other than null use:
OrderDate.Value != null
*For the requirement to use an OrderDate value for this year or a later year use:
OrderDate.Value>= year



Leave a Reply 10

Your email address will not be published. Required fields are marked *


Fippy Darkpaws

Fippy Darkpaws

NOT C because of: OrderDate.value.year == year and the requirement says “or a later year”.

Gehan

Gehan

The Correct answer is C

Kinnoy

Kinnoy

Ans. A
the requirement says “in or a later year”

Fippy Darkpaws

Fippy Darkpaws

OK, i see. There is no correct answer. OrderDate.Value != null will throw an invalid arguement exception (Value will get the non-null value) if it is null, so A could not be the answer either.

Flawed question.

PaulC

PaulC

the correct answer would be
Where order.OrderDate != null && order.OrderDate.Value.Year > = year
but its not listed in answers.

order.OrderDate.HasValue is wrong too, as you cant presume that the database field OrderDate is defined as nullable. Dont be misleaded by the public DateTime? OrderDate at the beginning of the code, which has nothing to do with the “orders”collection.

bob

bob

The answer really is ‘A’. The code provided defines the class ‘Order’. Just top and tail it like this…

[Table(Name=”Orders”)]
class Order
{
[Column]
public DateTime? OrderDate;
public IQueryable LookupOrdersForYear(int year)
{
using (var context = new DataContext(@”Data Source=.\;
Initial Catalog=Northwind;Integrated Security=SSPI;”))
{
var Orders =
from c in context.GetTable()
where c.OrderDate.Value != null && c.OrderDate.Value.Year >= year
select c;

return Orders.ToList().AsQueryable();
}
}
}

When you run this function it works correctly when using line from option A:
where order.OrderDate.Value != null …
It doesn’t throw an error.

This is because the ‘Nullable object must have a value’ error is thrown only when the OrderDate.Value is actually null. It’s not a compile time error. So for instance..

Order _ord = new Order(){OrderDate = datetime.now}
if(_ord.OrderDate.Value != null)

won’t throw the error.

Remember that with LINQ for SQL, what you’re writing in the query isn’t actually ever run as such, it’s converted into SQL and that is run against the database. As such the ‘order.OrderDate.Value != null’ never actually sees a null value and therefore no error.

ASV

ASV

The answer is –

Where order.OrderDate.HasValue && order.OrderDate.Value.Year > = year. It doesn’t present in Options.

Rao

Rao

In the older version (v.1) of the question there was this option as possible answer:

Where order.OrderDate.HasValue && order.OrderDate.Value.Year > = year

Maybe a wrong conversion in this dump?

Aydin_BHOS

Aydin_BHOS

Guys, everything is all right with this question.
The point that is going to be made here is that:
When we do not want to raise an exception we have to use OrderDate.HasValue. But as can be seen, here we are not asked to find a segment that does not throw an exception. Moreover, it could be a correct answer if there were “Where order.OrderDate.HasValue && order.OrderDate.Value.Year > = year” not “Where order.OrderDate.HasValue && order.OrderDate.Value.Year = = year”.