0

我看过很多地方,我在 C# 中看到过几个地方,但在 VB.NET 中什么也没有。所以这里是设置。我有 3 张桌子。它们的格式如下

Quiz
=========
ID
CourseID
PassingNumber

Question
==========
ID
QuizID
QuestionText

Answer
==========
ID
QuestionID
AnswerText
IsCorrect

我想查询它们将它们放入对象列表中。基本上我想这样做:

Dim quizzes = From qui In Quiz
Where qui.CouseID = courseID (This variable is passed from a function)
Select qui.ID, qui.PassingNumber
For Each qui.ID
From que In Question
Where que.QuizID = qui.ID
Select que.ID, que.QuestionText
For Each que.ID
From ans In Answer
Where ans.QuestionID = que.ID
Select ans.AnswerText, ans.IsCorrect

我知道 For Each 应该类似于 Join,但我似乎无法完全掌握这个概念。如果我看到它,我想我会的。

4

2 回答 2

0

事实证明,这个问题已经从另一个帖子中得到了回答。

LINQ:连接三个表的“组加入”功能

这就是我想要的,而且效果很好:

Dim orderProds = From order In cData.Orders _
      Group Join prod In cData.Products _
      On order.ProductID Equals prod.ProductID Into OrderProducts = Group

Dim customerOrderProducts = From cust In cData.Customers _
                        Group Join ordProd In orderProds _
                        On cust.CustomerID Equals ordProd.order.CustomerID Into custOrderProds = Group

适应我的代码:

Dim qAs = From que In _dataContext.Questions _
    Group Join ans In _dataContext.Answers _
    On ans.quiestionID Equals que.ID Into QuestionAnswers = Group

Dim qQAs = From qui In _dataContext.Quizs _ (<- Microsoft's pluralization for ya')
    Group Join qA In qAs _
    On qui.ID Equals qA.que.quizID Into QuizQuestionAnswers = Group _
    Where qui.courseID = courseID
于 2013-06-30T03:27:14.297 回答
0

根据您尝试做的最简单的方法可能会使用包括

   Dim q = db.Quiz.Where(q=>q.CouseID = 
courseID).Include("Question").Include("Question.Answer").ToList()
于 2013-06-27T01:57:40.220 回答