我是 LINQ 新手,我有这个查询,它返回调查中的页面。(无论出于何种原因,这些值都没有具体化到表中。)
//Group all of this data by page
var pages = from fq in db.FormQuestions
where (fq.FormId == id) && fq.Disabled == false
group fq by fq.PageNumber into p
select new DTOs.PageDTO { PageNumber = p.Key.Value };
然后我有这个查询,它将所有的叶子数据投影到 DTO 中。
var questions = from fq in db.FormQuestions
join q in db.Questions on fq.QuestionId equals q.QuestionId
where (fq.FormId == id) && fq.Disabled == false
//where (fq.FormId == id) && fq.Disabled == false && fq.PageNumber == page
orderby fq.DisplayOrder
select new DTOs.FormQuestionDTO()
{
DisplayOrder = (fq.DisplayOrder.HasValue ? fq.DisplayOrder.Value : 0),
PageNumber = (fq.PageNumber.HasValue ? fq.PageNumber.Value : 0),
QuestionId = q.QuestionId,
QuestionSelectionMode = q.vts_tbQuestionSelectionMode.Description,
QuestionText = q.QuestionText,
Answers =
from answer in q.Answers
join at in db.AnswerTypes on answer.AnswerTypeId equals at.AnswerTypeID
where answer.Disabled == false
orderby answer.DisplayOrder
select new DTOs.AnswerDTO()
{
AnswerId = answer.AnswerId,
AnswerText = answer.AnswerText,
DisplayOrder = answer.DisplayOrder,
AnswerType = at.Description
}
};
有没有办法巧妙地加入这两个?即,在每个页面 DTO 下,我想查看 QUEstion DTO,然后在该 Answer DTO 内部,然后……
此外,即使我可以在一个 LINQ 语句中完成所有这些操作,是否最好单独构建 LINQ 语句然后合并它们?这感觉有点像在 SQL 中构建临时变量,因为它可能构建一个巨大的查询,但维护起来很痛苦。