当一个函数是一个复杂的对象时,如何指定它的返回类型?

时间:2016-03-01 15:11:23

标签: c#

我有一个执行此操作的功能:

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()
                }
}

以下是问题组合的定义方式:

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>>

有人可以解释我如何定义此函数的返回类型。

1 个答案:

答案 0 :(得分:3)

您需要返回objectdynamic,但不是一个好主意, Intellisense 将无法使用,可能您必须使用 Reflection 获取值。

更好的方法是使用您想要的属性创建一个类,也许它在您的情况下有点复杂,但我认为这是最好的方法:

class QuestionGroup
{
    public string Answer { get; set; }
    public string AnswerGridCorrect { get; set; }
    public string AnswerGridResponses { get; set; }
    ...
}

并在您的方法中返回该类型:

public QuestionGroup GroupQuestions(questions) 
{
   return = 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 QuestionGroup
                {
                    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()
                }
}