我试图从 2 个表中在 Linq 中进行自然左连接
2张桌子
| questions |
+-----------+
| id |
| question |
+-----------+
| answers |
+-----------+
| id |
| q_id (fk)|
| answer |
+-----------+
我试图为每个问题检索一行,但可以根据需要使用其他列
我不确定这是否可能
ID | Question | Answer 1 | Answer 2 | An....
| view | |-----------------------------------------------+
+-----------+ | 1 | question | answer1 | answer2 | ... |
| id | | 2 | question | answer1 | answer2 | ... |
| question | | 3 | question | answer1 | answer2 | ... |
| answer1 | or | 4 | question | answer1 | answer2 | ... |
| answer2 | | 5 | question | answer1 | answer2 | ... |
| answer3 | | 6 | question | answer1 | answer2 | ... |
| answer... | | 7 | question | answer1 | answer2 | ... |
+-----------+ |-----------------------------------------------+
我的 C# Linq
var joinedTable =
from questions in db.Results
join answers in db.Answers on questions.id equals answers.result_id
into answers
select new
{
questions.id,
TOTAL_ANSWERS = answers.Count(),
questions.SurveyDateCreated,
(answers.ForEach(a=> a.answer) as "Answer" + i++)
};