【发布时间】:2016-03-01 15:11:23
【问题描述】:
我有一个功能可以做到这一点:
public GroupQuestions(questions) {
var questionsGrouped = questions
.GroupBy(
r => new {
r.Answer,
r.Answered,
r.AnswerGridCorrect,
r.AnswerGridResponses,
r.CorrectCount,
r.Hint,
r.IncorrectCount,
r.QuestionNumber,
r.QuestionUId,
r.Locked,
r.Result,
r.ShownCount,
r.Tagged,
r.Text,
r.UserTestQuestionId
},
(key, results) => new
{
Answer = key.Answer,
AnswerGridCorrect = key.AnswerGridCorrect,
AnswerGridResponses = key.AnswerGridResponses,
Answered = key.Answered,
CorrectCount = key.CorrectCount,
Hint = key.Hint,
IncorrectCount = key.IncorrectCount,
QuestionNumber = key.QuestionNumber,
QuestionUId = key.QuestionUId,
Locked = key.Locked,
Result = key.Result,
ShownCount = key.ShownCount,
Tagged = key.Tagged,
Text = key.Text,
UserTestQuestionId = key.UserTestQuestionId,
AnswerGrid = results
.Select((r, index) => new
{
AnswerId = r.AnswerId,
Text = r.AnswerText,
Correct = key.AnswerGridCorrect == null ? null : (bool?)Convert.ToBoolean(int.Parse(key.AnswerGridCorrect.Substring(index, 1))),
Response = key.AnswerGridResponses == null ? null : (bool?)Convert.ToBoolean(int.Parse(key.AnswerGridResponses.Substring(index, 1)))
})
.ToList()
}
}
这是 questionsGrouped 的定义方式:
System.Collections.Generic.List<<anonymous type: string Answer, string AnswerGridCorrect,
string AnswerGridResponses, bool Answered, int CorrectCount, string Hint, int IncorrectCount,
int QuestionNumber, System.Guid QuestionUId, bool Locked, string Result,
int ShownCount, bool Tagged, string Text, int UserTestQuestionId,
System.Collections.Generic.List<<anonymous type: int AnswerId, string Text, bool? Correct, bool? Response>> AnswerGrid>>
谁能解释我如何定义这个函数的返回类型。
【问题讨论】:
-
最好不要让类型匿名,而是
class或entity model
标签: c#