简体   繁体   English

Linq查询从三个表填充集合

[英]Linq query populate collection from three tables

I have the following three tables: 我有以下三个表:

[SURVEY_QUESTIONS]
(
[QuestionId]
[SurveyId],
[Question],
[AnswerType] 
)

[SURVEY_ANSWERS]
(
[AnswerId],
[QuestionId],
[ResponseId],
[Answer]
)

[SURVEY_RESPONSES]
(
[ResponseId],
[SurveyId]
)

I need to populate the following object and I have the ResponseId. 我需要填充以下对象,并且具有ResponseId。

public class SurveyResponse
{
    public int ResponseId { get; set; }

    public string Question { get; set; }

    public string Answer { get; set; }
}

I started writing this method which would return a collection of the SurveyResponse object, but am not sure how to finish or what would be the best method to return my collection 我开始编写此方法,该方法将返回SurveyResponse对象的集合,但不确定如何完成操作或返回我的集合的最佳方法是什么

    public static List<SurveyResponse> GetSurveyResponse(int responseId)
    {
        using (CaseDatabaseEntities entities = BaseDAL.GetNewEntities())
        {
            SURVEY_RESPONSES sr = entities.SURVEY_RESPONSES
                                                    .Where(r => r.ResponseId == responseId).FirstOrDefault();

            var Answers = entities.SURVEY_ANSWERS.Where(a => a.ResponseId == responseId);
            var Questions = entities.SURVEY_QUESTIONS.Where(q => q.SurveyId == sr.SurveyId);

            List<SurveyResponse> surveyResp = new List<SurveyResponse>();
            foreach (var q in Questions)
            {

            }

            return surveyResp;

        }
    }

I think this is what you are looking for. 我认为这就是您想要的。

public static List<SurveyResponse> GetSurveyResponse(int responseId)
{
    using (CaseDatabaseEntities entities = BaseDAL.GetNewEntities())
    {
        SURVEY_RESPONSES sr = entities.SURVEY_RESPONSES
                                                .Where(r => r.ResponseId == responseId).FirstOrDefault();

        var Answers = entities.SURVEY_ANSWERS.Where(a => a.ResponseId == responseId).;
        var Questions = entities.SURVEY_QUESTIONS.Where(q => q.SurveyId == sr.SurveyId);

        var questionsWithAnswers = (from q in Questions
                    join a in Answers on q.QuestionId equals a.QuestionId
                    select new SurveyResponse(){ ResponseId = a.responseId, Question = q.Question, Answer = a.Answer}).ToList()

        return questionsWithAnswers;

    }
}

I haven't tested the code. 我还没有测试代码。

This can best be done using joins in Linq. 最好使用Linq中的联接来完成。 Assuming the column "ResponseId" in the answer table corresponds to the column "ResponseId" in the survey responses table you could do the following: 假设答案表中的“ ResponseId”列对应于调查响应表中的“ ResponseId”列,您可以执行以下操作:

var responses = (from s in entities.SURVEY_RESPONSES 
                  join q in entities.SURVEY_QUESTIONS on s.SurveyId equals q.SurveyId
                  join a in entities.SURVEY_ANSWERS on s.ResponseId equals a.ResponseId
                  where s.ResponseId == responseId
                  select new {s.ResponseId, q.Question, a.Answer});

List<SurveyResponse> surveyResp = new List<SurveyResponse>();
foreach (var r in responses)
{
  SurveyResponse survey = new SurveyResponse();
  survey.ResponseId = r.ResponseId;
  survey.Question = r.Question;
  survey.Answer = r.Answer;
  surveyResp.Add(survey);
}

return surveyResp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM