有没有办法我只能从.Include中选择某些字段(在LINQ查询中?

时间:2014-01-07 06:53:49

标签: c# linq

鉴于以下内容:

var questions = _questionsRepository
   .GetAll()
   .Where(q => q.Problem.SubTopicId == subTopicId || subTopicId == 0)
   .Where(q => q.QuestionStatusId == questionStatusId || questionStatusId == 0)
   .Where(q => q.AssignedTo == assignedTo || assignedTo == "0")
   .Where(q => q.ModifiedBy == modifiedBy || modifiedBy == "0")
   .Include(q => q.Problem)
   .Include(q => q.Answers)
   .ToList();

有没有办法可以在问题表/类中返回问题和答案数据以及字段SubTopicIdProblemId

1 个答案:

答案 0 :(得分:3)

尝试在查询结尾添加select

var problems = _questionsRepository
            .GetAll()
            .Where(q => q.Problem.SubTopicId == subTopicId || subTopicId == 0)
            .Where(q => q.QuestionStatusId == questionStatusId || questionStatusId == 0)
            .Where(q => q.AssignedTo == assignedTo || assignedTo == "0")
            .Where(q => q.ModifiedBy == modifiedBy || modifiedBy == "0")
            .Include(q => q.Problem)
            .Include(q => q.Answers)
            .Select(x=>new 
               {
                   SubTopicId = x.Problem.SubTopicId, 
                   ProblemId = x.Problem.ProblemId 
               }).ToList();