Which code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application
that uses the Entity Framework. The application has an entity model that contains a
SalesOrderHeader entity. The entity includes an OrderDate property of type DateTime. You need to
retrieve the 10 oldest SalesOrderHeaders according to the OrderDate property. Which code segment
should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application
that uses the Entity Framework. The application has an entity model that contains a
SalesOrderHeader entity. The entity includes an OrderDate property of type DateTime. You need to
retrieve the 10 oldest SalesOrderHeaders according to the OrderDate property. Which code segment
should you use?

A.
var model = new AdventureWorksEntities();
var sales = model.SalesOrderHeaders.Take(10).OrderByDescending(soh => soh.OrderDate);

B.
var model = new AdventureWorksEntities();
var sales = model.SalesOrderHeaders.OrderByDescending(soh => soh.OrderDate).Take(10);

C.
var model = new AdventureWorksEntities();
var sales = model.SalesOrderHeaders.OrderBy(soh => soh.OrderDate).Take(10);

D.
var model = new AdventureWorksEntities();
var sales = model.SalesOrderHeaders.Take(10).OrderBy(soh => soh.OrderDate);

Explanation:

OrderBy() Sorts the elements of a sequence in ascending order according to a key.
OrderByDescending() Sorts the elements of a sequence in descending order according to a key.
Enumerable.OrderBy<TSource, TKey> Method (IEnumerable<TSource>, Func<TSource, TKey>)
(http://msdn.microsoft.com/en-us/library/bb534966.aspx)



Leave a Reply 0

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