Which code segment should you use?

Case Study: 1
Scenario 1
Background
You are developing a flight information consolidation service. The service retrieves flight information from a
number of sources and combines them into a single data set. The consolidated flight information is stored in a
SQL Server database. Customers can query and retrieve the data by using a REST API provided by the
service. The service also offers access to historical flight information. The historical flight information can be
filtered and queried in an ad hoc manner. The service runs on a Windows Azure Web Role. SSL is not used.
Business Requirements
A new data source for historical flight information is being developed by a contractor located on another
continent.
If a time zone is not specified, then it should be interpreted as Coordinated Universal Time (UTC).
When you upgrade a service from a staging deployment to a production deployment, the time that the service
is unavailable must be minimized.
The default port must be used for HTTP.
Technical Requirements
The existing sources of flight information and the mechanism of exchange are listed below.
Blue Yonder Airlines provides flight information in an XML file.
Consolidated Messenger provides flight information in a Microsoft Access database that is uploaded every 12
hours to the service using SFTP. The company uses port 22 for SFTP.
Margie’s Travel provides and consumes flight information using serialized ADO.NET DataSets. Data is
periodically synced between the service and Margie’s Travel.
Trey Research provides data from multiple sourcesserialized in proprietary binary formats. The datamust be
read by using .NET assemblies provided by Trey Research. The assemblies use a common set of
dependencies. The current version of the Trey Research assemblies is 1.2.0.0. All assemblies provided by
Trey Research are signed with a key pair contained in a file named Trey.snk, which Trey Research also
supplies.
The application specification requires that any third-party assemblies must have strong names.
Application Structure




********************************************************

You are adding a new REST service endpoint to the FlightDataController controller. It returns flights from the
consolidated data sources only for flights that arelate. You need to write a LINQ to Entities query to extract the
required data. Which code segment should you use?

Case Study: 1
Scenario 1
Background
You are developing a flight information consolidation service. The service retrieves flight information from a
number of sources and combines them into a single data set. The consolidated flight information is stored in a
SQL Server database. Customers can query and retrieve the data by using a REST API provided by the
service. The service also offers access to historical flight information. The historical flight information can be
filtered and queried in an ad hoc manner. The service runs on a Windows Azure Web Role. SSL is not used.
Business Requirements
A new data source for historical flight information is being developed by a contractor located on another
continent.
If a time zone is not specified, then it should be interpreted as Coordinated Universal Time (UTC).
When you upgrade a service from a staging deployment to a production deployment, the time that the service
is unavailable must be minimized.
The default port must be used for HTTP.
Technical Requirements
The existing sources of flight information and the mechanism of exchange are listed below.
Blue Yonder Airlines provides flight information in an XML file.
Consolidated Messenger provides flight information in a Microsoft Access database that is uploaded every 12
hours to the service using SFTP. The company uses port 22 for SFTP.
Margie’s Travel provides and consumes flight information using serialized ADO.NET DataSets. Data is
periodically synced between the service and Margie’s Travel.
Trey Research provides data from multiple sourcesserialized in proprietary binary formats. The datamust be
read by using .NET assemblies provided by Trey Research. The assemblies use a common set of
dependencies. The current version of the Trey Research assemblies is 1.2.0.0. All assemblies provided by
Trey Research are signed with a key pair contained in a file named Trey.snk, which Trey Research also
supplies.
The application specification requires that any third-party assemblies must have strong names.
Application Structure




********************************************************

You are adding a new REST service endpoint to the FlightDataController controller. It returns flights from the
consolidated data sources only for flights that arelate. You need to write a LINQ to Entities query to extract the
required data. Which code segment should you use?

A.
Option A

B.
Option B

C.
Option C

D.
Option D

Explanation:
D is right because you send result as REST so if you use AsQueryable the result is deferred to the next
enumeration of your result.
D is not optimized but will works.
A will break at runtime.



Leave a Reply 3

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


aleksey

aleksey

Correct D, but explanation is wrong! Both A and D give you deffered query, but A throws exception “Unable to create a constant value of type ‘EDM.Client.FlightInfo’. Only primitive types or enumeration types are supported in this context.” when you try to enumerate it. Because in A you try to mix iquerable as outer with ienumerable as inner into join.

bob

bob

I’m confused. Both of the following work for me!

IEnumerable hist = historical.AsEnumerable();

var query = LoadFlights().AsQueryable()
.Join(hist, x => x.Flight, y => y.Flight, (x, y) => new
{
Current = x,
Historical = y
})
.Where(ax => ax.Historical.WasLate)
.Select(bx => bx.Current);

also

IEnumerable hist = historical.AsEnumerable();

var query = LoadFlights().AsEnumerable()
.Join(hist, x => x.Flight, y => y.Flight, (x, y) => new
{
Current = x,
Historical = y
})
.Where(ax => ax.Historical.WasLate)
.Select(bx => bx.Current);

artvig

artvig

D is correct – A returns an EnumerableQuery whereas D returns an IEnumerable which matches the requirement.

Quick linqpad demo:

void Main()
{
var historical = new HistoricalDataLoader().LoadHistorical();

var Db = new DbContext();

// A
var query = Db.Flights.AsQueryable()
.Join(historical, x=>x.Flight, y=> y.Flight, (x, y) => new { Current = x, Historical = y})
.Where(x=>x.Historical.WasLate)
.Select(x=>x.Current);
query.Dump(“A”);

// D
var query2 = Db.Flights.AsEnumerable()
.Join(historical, x=> x.Flight, y=>y.Flight, (x,y) => new { Current= x, Historical = y})
.Where(x=>x.Historical.WasLate)
.Select(x=>x.Current);
query2.Dump(“D”);
}

public class FlightInfo {
public string Airline {get;set;}
public string Flight {get;set;}
public bool WasLate {get;set;}
}

public class HistoricalDataLoader
{
public IEnumerable LoadHistorical()
{
yield return new FlightInfo { Airline = “BA”, Flight = “BA1”, WasLate = false };
yield return new FlightInfo { Airline = “BA”, Flight = “BA2”, WasLate = true };
yield return new FlightInfo { Airline = “BA”, Flight = “BA3”, WasLate = false };
yield return new FlightInfo { Airline = “Qantas”, Flight = “Q1”, WasLate = false };
yield return new FlightInfo { Airline = “Qantas”, Flight = “Q2”, WasLate = true };
yield return new FlightInfo { Airline = “Qantas”, Flight = “Q3”, WasLate = true };
}
}

public class DbContext {
public IQueryable Flights { get {
return GetCurrent().AsQueryable();
} set {} }

private IEnumerable GetCurrent()
{
yield return new FlightInfo { Airline = “BA”, Flight = “BA1”, WasLate = false };
yield return new FlightInfo { Airline = “BA”, Flight = “BA2”, WasLate = false };
yield return new FlightInfo { Airline = “BA”, Flight = “BA3”, WasLate = true };
yield return new FlightInfo { Airline = “Qantas”, Flight = “Q2”, WasLate = true };
}
}