加入组查询



我是LINQ Newb,我已经得到了这个查询,它在调查中返回页面。(无论出于何种原因,这些值都没有具体化到表中。)

//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下面,我想看到问题DTO,然后在答案DTO里面,然后…

另外,即使我可以在一个LINQ语句中完成所有这些,单独构建LINQ语句然后合并它们是否更好?这感觉有点像在SQL中构建临时变量,因为可以构建一个巨大的查询,但维护起来很麻烦。

那么为什么这样的东西不起作用呢?

//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 
          questions = from fq in db.FormQuestions
                     join q in db.Questions on fq.QuestionId equals q.QuestionId
                     where (fq.FormId == id) && fq.Disabled == false
                     orderby fq.DisplayOrder
                     select new DTOs.FormQuestionDTO()
                     {
                       DisplayOrder = (fq.DisplayOrder.HasValue ? fq.DisplayOrder.Value : 0),
                       // etc as you 2nd code sample

相关内容

  • 没有找到相关文章

最新更新