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?

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?

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});



Leave a Reply 6

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


E

E

What is wrong with C and D?
Is the distinct() call in C redundant?

j

j

C and D would return data from sales territories

Gaius

Gaius

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.

Mike

Mike

A and D both work as long as SalesPersons is not null for all territories. If SalesPersons is null for any territory then D fails.

Artem

Artem

I think C, D fails because these queries couldn’t be parsed by EF QueryProvider.

Piotrus

Piotrus

A would not work if SalesTerritory was null for the Person (0 on the end of Sales Territory).
I’d say D, because of required column names.