这是在Visual Basic应用程序中使用LINQ to SQL的家庭作业。除了我有一个部分破碎的结果外,它在大多数方面都是正确的。我的代码不是将我的第二个查询的结果添加到我的列表框中,而是添加了查询本身的奇怪表示。下面是DB的描述,后面是我的输出(预期和实际),最后是我的代码。请指出我破碎的概念,这样我就可以弄清楚我错过了什么。非常感谢。
我在一个数据库中使用两个名为Members and Payments的表。成员有一个名为ID
的主键,还有字段first_name
和last_name
。付款有一个名为Members_Id
的外键,它与会员的主键相关联;付款还包含Payments
列下的付款值。
输出应该是这样的
Member name = John Smith
$48.00, 10/20/2005
$44.00, 3/11/2006
但是这是
Member name = SELECT ([t0].[First_Name] + @p1) + [t0].[Last_Name]
AS [value]FROM[dbo].[Members] AS [t0].[ID] = @p0
$48.00, 10/20/2005
$44.00, 3/11/2006
Public Class FormPaymentsGroup
Private db As New KarateClassesDataContext
Private Sub FormPaymentsGroup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.PaymentsTableAdapter.Fill(Me.KarateDataSet.Payments)
'Group payments by Member_ID (FKey) in the Payments table. (Working fine)
Dim IdQuery = From aMember In db.Payments
Group aMember By aMember.Member_Id
Into MemberPayments = Group
For Each memberID In IdQuery
' Use the passed member_Id to target the row in the Members table and return the first_name & last_name.
' PROBLEM: This only seems to be returning the query itself; not the result.
Dim currMemberID = memberID.Member_Id
Dim nameQuery = From aName In db.Members
Where aName.ID = currMemberID
Select aName.First_Name + " " + aName.Last_Name
Dim currName = nameQuery.ToString ' Load the query result into a portable variable.
LbxMemberPayments.Items.Add("Member name = " & currName) ' PROBLEM: This is where the name SHOULD BE posted to the listbox.
' This is bound to the Members table but directs it based on the above IdQuery.
For Each enteredPayment In memberID.MemberPayments
LbxMemberPayments.Items.Add(vbTab & FormatCurrency(enteredPayment.Amount) & ", " & enteredPayment.Payment_Date)
Next
LbxMemberPayments.Items.Add(vbCr) ' Carriage return formatting
Next
End Sub
End Class
答案 0 :(得分:0)
更改
Dim currName = nameQuery.ToString
到
Dim currName = nameQuery.FirstOrDefault