PrepAway - Latest Free Exam Questions & Answers

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 the entity model shown in the following diagram.

The application must create a projection of the unique set of names and year-to-date sales for territories where at least one sales person had sales last year of more than $100,000.
The projection must consist of properties named Sales and Name. You need to write a query that will generate the required projection.
Which code segment should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
(from person in model.SalesPersons
where (person.SalesLastYear > 100000)
select new {
Name = person.SalesTerritory.Name,
Sales = person.SalesTerritory.SalesYTD
}
).Distinct();

B.
(from person in model.SalesPersons
where (person.SalesLastYear > 100000)
select new {
Name = person.SalesTerritory.Name,
Sales = person.SalesTerritory.SalesYTD
}
);

C.
model.SalesTerritories.Where( t => t.SalesPersons.Any( p => p.SalesLastYear > 100000))
.Select( t=> new { t.Name, t.SalesYTD})
.Distinct();

D.
model.SalesTerritories.Where( t=> t.SalesPersons.Any( p => p.SalesLastYear > 100000))
.Select( t=> new { t.Name, Sales = t.SalesYTD});

6 Comments on “Which code segment should you use?

  1. Gaius says:

    A would work, but it seems less efficient than D to me, since it will require a pass to filter out duplicates.

    B will not work, because it will return duplicates.

    The query in C not only has a redundant Distinct() call, but it returns a projection w/ the properties Name and SalesYTD instead of Name and Sales.

    D looks fine to me, but I will have to run it to make sure.




    0



    0

Leave a Reply