【问题标题】:How can I specify the return type of a function when it's a complex object? [duplicate]当函数是复杂对象时,如何指定函数的返回类型? [复制]
【发布时间】: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>>

谁能解释我如何定义这个函数的返回类型。

【问题讨论】:

  • 最好不要让类型匿名,而是classentity model

标签: c#


【解决方案1】:

您需要返回 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()
                }
}

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2020-11-01
    • 2017-06-20
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多