You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to a Microsoft SQL Server 2008 database. You create classes by using LINQ to
SQL based on the records shown in the exhibit. (Click the Exhibit button.)
You need to create a LINQ query to retrieve a list of objects that contains the OrderID and
CustomerID properties. You need to retrieve the total price amount of each Order record. What are
two possible ways to achieve this goal? (Each correct answer presents a complete solution. Choose
two.)
A.
From details in dataContext.Order_Details _
Group details By details.OrderID Into g _
Join order In dataContext.Orders On g.Key = order.OrderID _
Select New With { _
.OrderID = order.OrderID, _
.CustomerID = order.CustomerID, _
.TotalAmount = g.Sum(Function(od) od.UnitPrice * od.Quantity) _
}
This is an Object Query. It looks at the Order Details EntitySet and creating a group g based on
OrderID.
* The group g is then joined with Orders EntitySet based on g.Key = OrderID
* For each matching records a new dynamic object containing OrderID, CustomerID and
TotalAmount is created.
* The dynamic records are the results returned in an INumerable Object, for later processing
Alterantive D. This is an Object Query. The GroupJoin method is used to join Orders to OrderDetails.
Parameters for GroupJoin:
1. An Order_Details EntitySet
2. Order o (from the Orders in the Orders Entity Set, picking up Order_id from both Entity Sets)
3. Order_ID from the first Order_Details record from the OD EnitySet
4. Lamda Expression passing ord and dts (ord=o, dts=d)
The body of the Lamda Expression is working out the total and Returning a Dynamic object as in A.
QUESTION 36
You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application uses the ADO.NET Entity Framework to model entities. The conceptual schema definition
language (CSDL) file contains the following XML fragment.
<EntityType Name=”Contact”>
…
<Property Name=”EmailPhoneComplexProperty”
Type=”AdventureWorksModel.EmailPhone”
Nullable=”false” />
</EntityType>
…
<ComplexType Name=”EmailPhone”>
<Property Type=”String” Name=”EmailAddress”
MaxLength=”50″ FixedLength=”false”
Unicode=”true” />
<Property Type=”String” Name=”Phone” MaxLength=”25″
FixedLength=”false” Unicode=”true” />
</ComplexType>
You write the following code segment. (Line numbers are included for reference only.)
01 Using conn As New EntityConnection(“name=AdvWksEntities”)
02 conn.Open()
03 Dim esqlQuery As String = “SELECT VALUE contacts FROM” &
04 ” AdvWksEntities.Contacts AS contacts” &
05 ” WHERE contacts.ContactID == 3″
06 Using cmd As EntityCommand = conn.CreateCommand()
07 cmd.CommandText = esqlQuery
08 Using rdr As EntityDataReader = cmd.ExecuteReader()
09 While rdr.Read()
10
11 End While
12 End Using
13 End Using
14 conn.Close()
15 End Using
You need to ensure that the code returns a reference to a ComplexType entity in the model named
EmailPhone. Which code segment should you insert at line 10?
Dim FldIdx As Integer = 0
Dim key As EntityKey = TryCast(record.GetValue(FldIdx), EntityKey)
For Each keyMember As EntityKeyMember In key.EntityKeyValues
Return keyMember.Key + ” : ” + keyMember.Value
Next
B.
dataContext.Order_Details.GroupJoin(dataContext.Orders, Function(d) d.OrderID, Function(o)
o.OrderID, Function(dts,ord) New With { _
.OrderID = dts.OrderID, _
.CustomerID = dts.Order.CustomerID, _
.TotalAmount = dts.UnitPrice * dts.Quantity _
})
Dim record As IExtendedDataRecord = TryCast(rdr(“EmailPhone”), IExtendedDataRecord)
Dim FldIdx As Integer = 0
Return record.GetValue(FldIdx)
C.
From order in dataContext.Orders _
Group order By order.OrderID Into g _
Join details in dataContext.Order_Details On g.Key = details.OrderID _
Select New With { _
.OrderID = details.OrderID, _
.CustomerID = details.Order.CustomerID, _
.TotalAmount = details.UnitPrice * details.Quantity _
}
Dim nestedRecord As DbDataRecord = TryCast(rdr(“EmailPhoneComplexProperty”),
DbDataRecord) Return nestedRecord
D.
dataContext.Orders.GroupJoin(dataContext.Order_Details, Function(o) o.OrderID, Function(d)
d.OrderID, Function(ord, dts) New With { _
.OrderID = ord.OrderID, _
.CustomerID = ord.CustomerID, _
.TotalAmount = dts.Sum(Function(od) od.UnitPrice * od.Quantity) _
})
Dim fieldCount As Integer = rdr(“EmailPhone”).DataRecordInfo.FieldMetadata.Count
Dim FldIdx As Integer = 0
While FldIdx < fieldCount
rdr.GetName(FldIdx)
If rdr.IsDBNull(FldIdx) = False Then
Return rdr(“EmailPhone”).GetValue(FldIdx).ToString()
End If
System.Math.Max(System.Threading.Interlocked.Increment(FldIdx),FldIdx – 1)
End While
A.
From details in dataContext.Order_Details _
Group details By details.OrderID Into g _
Join order In dataContext.Orders On g.Key = order.OrderID _
Select New With { _
.OrderID = order.OrderID, _
.CustomerID = order.CustomerID, _
.TotalAmount = g.Sum(Function(od) od.UnitPrice * od.Quantity) _
}
This is an Object Query. It looks at the Order Details EntitySet and creating a group g based on
OrderID.
* The group g is then joined with Orders EntitySet based on g.Key = OrderID
* For each matching records a new dynamic object containing OrderID, CustomerID and
TotalAmount is created.
* The dynamic records are the results returned in an INumerable Object, for later processing
Alterantive D. This is an Object Query. The GroupJoin method is used to join Orders to OrderDetails.
Parameters for GroupJoin:
1. An Order_Details EntitySet
2. Order o (from the Orders in the Orders Entity Set, picking up Order_id from both Entity Sets)
3. Order_ID from the first Order_Details record from the OD EnitySet
4. Lamda Expression passing ord and dts (ord=o, dts=d)
The body of the Lamda Expression is working out the total and Returning a Dynamic object as in A.
QUESTION 36
You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application uses the ADO.NET Entity Framework to model entities. The conceptual schema definition
language (CSDL) file contains the following XML fragment.
<EntityType Name=”Contact”>
…
<Property Name=”EmailPhoneComplexProperty”
Type=”AdventureWorksModel.EmailPhone”
Nullable=”false” />
</EntityType>
…
<ComplexType Name=”EmailPhone”>
<Property Type=”String” Name=”EmailAddress”
MaxLength=”50″ FixedLength=”false”
Unicode=”true” />
<Property Type=”String” Name=”Phone” MaxLength=”25″
FixedLength=”false” Unicode=”true” />
</ComplexType>
You write the following code segment. (Line numbers are included for reference only.)
01 Using conn As New EntityConnection(“name=AdvWksEntities”)
02 conn.Open()
03 Dim esqlQuery As String = “SELECT VALUE contacts FROM” &
04 ” AdvWksEntities.Contacts AS contacts” &
05 ” WHERE contacts.ContactID == 3″
06 Using cmd As EntityCommand = conn.CreateCommand()
07 cmd.CommandText = esqlQuery
08 Using rdr As EntityDataReader = cmd.ExecuteReader()
09 While rdr.Read()
10
11 End While
12 End Using
13 End Using
14 conn.Close()
15 End Using
You need to ensure that the code returns a reference to a ComplexType entity in the model named
EmailPhone. Which code segment should you insert at line 10?
Dim FldIdx As Integer = 0
Dim key As EntityKey = TryCast(record.GetValue(FldIdx), EntityKey)
For Each keyMember As EntityKeyMember In key.EntityKeyValues
Return keyMember.Key + ” : ” + keyMember.Value
Next
A.
From details in dataContext.Order_Details _
Group details By details.OrderID Into g _
Join order In dataContext.Orders On g.Key = order.OrderID _
Select New With { _
.OrderID = order.OrderID, _
.CustomerID = order.CustomerID, _
.TotalAmount = g.Sum(Function(od) od.UnitPrice * od.Quantity) _
}
This is an Object Query. It looks at the Order Details EntitySet and creating a group g based on
OrderID.
* The group g is then joined with Orders EntitySet based on g.Key = OrderID
* For each matching records a new dynamic object containing OrderID, CustomerID and
TotalAmount is created.
* The dynamic records are the results returned in an INumerable Object, for later processing
Alterantive D. This is an Object Query. The GroupJoin method is used to join Orders to OrderDetails.
Parameters for GroupJoin:
1. An Order_Details EntitySet
2. Order o (from the Orders in the Orders Entity Set, picking up Order_id from both Entity Sets)
3. Order_ID from the first Order_Details record from the OD EnitySet
4. Lamda Expression passing ord and dts (ord=o, dts=d)
The body of the Lamda Expression is working out the total and Returning a Dynamic object as in A.
QUESTION 36
You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application uses the ADO.NET Entity Framework to model entities. The conceptual schema definition
language (CSDL) file contains the following XML fragment.
<EntityType Name=”Contact”>
…
<Property Name=”EmailPhoneComplexProperty”
Type=”AdventureWorksModel.EmailPhone”
Nullable=”false” />
</EntityType>
…
<ComplexType Name=”EmailPhone”>
<Property Type=”String” Name=”EmailAddress”
MaxLength=”50″ FixedLength=”false”
Unicode=”true” />
<Property Type=”String” Name=”Phone” MaxLength=”25″
FixedLength=”false” Unicode=”true” />
</ComplexType>
You write the following code segment. (Line numbers are included for reference only.)
01 Using conn As New EntityConnection(“name=AdvWksEntities”)
02 conn.Open()
03 Dim esqlQuery As String = “SELECT VALUE contacts FROM” &
04 ” AdvWksEntities.Contacts AS contacts” &
05 ” WHERE contacts.ContactID == 3″
06 Using cmd As EntityCommand = conn.CreateCommand()
07 cmd.CommandText = esqlQuery
08 Using rdr As EntityDataReader = cmd.ExecuteReader()
09 While rdr.Read()
10
11 End While
12 End Using
13 End Using
14 conn.Close()
15 End Using
You need to ensure that the code returns a reference to a ComplexType entity in the model named
EmailPhone. Which code segment should you insert at line 10?
Dim FldIdx As Integer = 0
Dim key As EntityKey = TryCast(record.GetValue(FldIdx), EntityKey)
For Each keyMember As EntityKeyMember In key.EntityKeyValues
Return keyMember.Key + ” : ” + keyMember.Value
Next
B.
dataContext.Order_Details.GroupJoin(dataContext.Orders, Function(d) d.OrderID, Function(o)
o.OrderID, Function(dts,ord) New With { _
.OrderID = dts.OrderID, _
.CustomerID = dts.Order.CustomerID, _
.TotalAmount = dts.UnitPrice * dts.Quantity _
})
Dim record As IExtendedDataRecord = TryCast(rdr(“EmailPhone”), IExtendedDataRecord)
Dim FldIdx As Integer = 0
Return record.GetValue(FldIdx)
C.
From order in dataContext.Orders _
Group order By order.OrderID Into g _
Join details in dataContext.Order_Details On g.Key = details.OrderID _
Select New With { _
.OrderID = details.OrderID, _
.CustomerID = details.Order.CustomerID, _
.TotalAmount = details.UnitPrice * details.Quantity _
}
Dim nestedRecord As DbDataRecord = TryCast(rdr(“EmailPhoneComplexProperty”),
DbDataRecord) Return nestedRecord
D.
dataContext.Orders.GroupJoin(dataContext.Order_Details, Function(o) o.OrderID, Function(d)
d.OrderID, Function(ord, dts) New With { _
.OrderID = ord.OrderID, _
.CustomerID = ord.CustomerID, _
.TotalAmount = dts.Sum(Function(od) od.UnitPrice * od.Quantity) _
})
Dim fieldCount As Integer = rdr(“EmailPhone”).DataRecordInfo.FieldMetadata.Count
Dim FldIdx As Integer = 0
While FldIdx < fieldCount
rdr.GetName(FldIdx)
If rdr.IsDBNull(FldIdx) = False Then
Return rdr(“EmailPhone”).GetValue(FldIdx).ToString()
End If
System.Math.Max(System.Threading.Interlocked.Increment(FldIdx),FldIdx – 1)
End While
Explanation:
Alterantive