LINQ to Entities无法识别方法:LastOrDefault

时间:2016-09-25 22:16:57

标签: c# linq asp.net-mvc-5 entity-framework-6

概述: 在CompletedQuestions表中,UserId对应于完成该问题的用户。 Id属性对应于Questions表中的一个问题。我知道,我没有正确指定关系。但我不是很有经验。我只想完成一个项目,然后一旦我了解更多,我就会回来修复那些糟糕的编码实践。我无法理解以下异常。

n = $(`#${neighbor.id}`);

发生异常的操作:

LINQ to Entities does not recognize the method 'Riddle.Models.CompletedQuestion LastOrDefault[CompletedQuestion](System.Linq.IQueryable`1[Riddle.Models.CompletedQuestion])' method, and this method cannot be translated into a store expression.

    Line 46:                 if (RiddleCompleted == null)
    Line 47:                 {
    Line 48:                     var lastCompletedQuestion = _db.CompletedQuestions.Where(q => q.UserId == currentUserId) // exception occurs in this line
    Line 49:                                                                              .OrderBy(q => q.QuestionNumber)
    Line 50:                                                                              .LastOrDefault();

CompletedQuestion模型:

public ActionResult Riddle(int Id)
        {
            string currentUserId = User.Identity.GetUserId();
            var riddle = _db.Riddles.Where(r => r.Id == Id).Single();

            if (riddle.User.Id == User.Identity.GetUserId())
            {
                var question = riddle.Questions.Where(q => q.QuestionNumber == 1).SingleOrDefault();
                if (question == null)
                {
                    return View("NoMoreQuestions");
                }
                return View("RiddleOwner", question);
            }
            else
            {

                var RiddleCompleted = _db.CompletedRiddles.Where(r => r.Id == Id && r.UserId == currentUserId).SingleOrDefault();
                if (RiddleCompleted == null)
                {
                    var lastCompletedQuestion = _db.CompletedQuestions.Where(q => q.UserId == currentUserId)  // exception occurs in this line
                                                                             .OrderBy(q => q.QuestionNumber)
                                                                             .LastOrDefault();
                    if (lastCompletedQuestion == null)
                    {
                        var question = riddle.Questions.Where(q => q.QuestionNumber == 1).Single();
                        return View(question);
                    }

                    else
                    {
                        var question = riddle.Questions.Where(q => q.QuestionNumber == lastCompletedQuestion.QuestionNumber + 1).SingleOrDefault();
                        return View(question);
                    }
                }
                else
                {
                    return View("NoMoreQuestions");
                }
            }
        }

问题模型:

public class CompletedQuestion
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public int QuestionNumber { get; set; }
}

2 个答案:

答案 0 :(得分:3)

Linq To Entities不支持LastOrDefault()。因此它可以在内存中处理集合,但不会在您尝试查询数据库时使用。

这是处理它的有效方法:

var lastCompletedQuestion = 
_db.CompletedQuestions.Where(q => q.UserId == currentUserId)
.OrderByDescending(q => q.QuestionNumber)
.FirstOrDefault() 

                                                                         ;

答案 1 :(得分:2)

实体框架/ linq2sql的工作原理是将已编译的C#/ IL转换为SQL。它只能转换它知道的方法。该错误告诉您LastOrDefault不是其中之一。

你可以通过在LastOrDefault之前放置.ToList()来解决这个问题,它将它从sql-converter转移到vanilla C#中,你得到了LastOrDefault的常规工作版本。或者您可以翻转订单并使用它可以翻译的FirstOrDefault。