在c#linq中,可以在匿名选择中指定值

时间:2016-03-02 07:49:43

标签: c# linq

这是我的代码:

   userQuizzes = user.UserQuizes.OrderBy(uq => uq.VerbalQuizStartDate).Select(uq => new
                {
                    correctAnswersCount = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count(),
                    incorrectAnswersCount = 80 -  correctAnswersCount
                })

是否可以在select中获取correctAnswersCount值并将其用于其他变量?例如,现在我正在计算correctAnswersCount两次,因为上面的代码不起作用:

  userQuizzes = user.UserQuizes.OrderBy(uq => uq.VerbalQuizStartDate).Select(uq => new
                {
                    correctAnswersCount = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count(),
                    incorrectAnswersCount =  80 - uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count(),
                })

4 个答案:

答案 0 :(得分:2)

您可以使用这样的代码块:

userQuizzes = user.UserQuizes.OrderBy(uq => uq.VerbalQuizStartDate).Select(uq => 
{
    var corrAnswersCount = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count();
    return new 
    {
        correctAnswersCount = corrAnswersCount,
        incorrectAnswersCount = 80 -  corrAnswersCount
    }
})

或者,如果您更喜欢查询语法:

userQuizzes = from quiz in user.UserQuizes
              order by quiz.VerbalQuizStartDate
              let count = quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count()
              select new 
              {
                  correctAnswersCount = count,
                  incorrectAnswersCount = 80 - count
              }

答案 1 :(得分:1)

您可以使用do:

userQuizzes = user.UserQuizes.OrderBy(uq => uq.VerbalQuizStartDate).Select(uq =>
{ 
       var count = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count();
       return new
                    {
                        correctAnswersCount = count,
                        incorrectAnswersCount = 80 -  count
                    }
});

答案 2 :(得分:1)

您可以将任何临时结果存储在lambda函数体中的变量中,例如:

userQuizzes = user.UserQuizes
               .OrderBy(uq => uq.VerbalQuizStartDate)
               .Select(uq => 
                           { 
                               var count = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count();
                               //var temp2 = other calculations or something if You want...;
                               return new
                               {
                                   correctAnswersCount = count,
                                   incorrectAnswersCount =  80 - count
                               }
                           }
               );

答案 3 :(得分:0)

您可以使用lambda函数,在创建匿名对象之前可以在其中计算correctAnswersCount,然后将其用于两个属性:

userQuizzes = user.UserQuizes.OrderBy(uq => uq.VerbalQuizStartDate).Select(uq => {
    var correctAnswersCount = uq.Quiz.MathQuizes.Where(m => m.ISMovedAnswerCorrect).Count() + uq.Quiz.VerbalQuizes.Where(v => v.ISMovedAnswerCorrect).Count();
    return new 
    {
        correctAnswersCount = correctAnswersCount ,
        incorrectAnswersCount = 80 - correctAnswersCount
    };
});