Skip to content

Instantly share code, notes, and snippets.

@chrisrichards
Created October 19, 2011 13:05
Show Gist options
  • Save chrisrichards/1298222 to your computer and use it in GitHub Desktop.
Save chrisrichards/1298222 to your computer and use it in GitHub Desktop.
Create an object model from a single LINQ query
var results = Session.Query<Answer>()
.Select(answer => new
{
QuestionId = answer.Question.Id,
QuestionText = answer.Question.Text,
QuestionDate = answer.Question.Date,
AnswerId = answer.Id,
AnswerText = answer.Text,
AnswerDate = answer.Date,
})
.OrderBy(result => result.QuestionDate).ThenBy(result => result.AnswerDate)
.ToList();
IList<QuestionViewModel> questions = new List<QuestionViewModel>();
QuestionViewModel questionViewModel = null;
foreach (var result in results) {
if (questionViewModel == null || questionViewModel.Id != result.QuestionId) {
questionViewModel = new questionViewModel { Id = result.QuestionId, Text = dto.QuestionText };
questions.Add(questionViewModel);
}
var answerViewModel = new AnswerViewModel
{
Id = dto.AnswerId,
Text = dto.AnswerText,
};
questionViewModel.Answers.Add(answerViewModel);
}
return questions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment