LINQ:为什么我在这个LINQ查询上出错?

Obs*_*vus 2 linq asp.net-mvc repository linq-to-sql asp.net-mvc-3

这是我在我的存储库中的方法:

    public List<Question> getallquestion()
    {
        var hej = from Core in db.CoreValue
                    join Question in db.Question on Core.CID equals question.QID
                    join Subject in db.SubjectType on Core.CID equals Subject.SID
                    select new
                    {
                        Core.Cname,
                        Question.QuestionText,
                        Subject.Sname
                    };

        return hej.ToList();

    }
Run Code Online (Sandbox Code Playgroud)

这是错误:

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'NKI3.Models.Question'
Run Code Online (Sandbox Code Playgroud)

这个错误的解决方案是什么?我似乎无法找到它

我的CreateViewModel:

    public class CreateViewModel
    {
        public string QuestionText { get; set; }
        public string Sname { get; set; }
        public string Cname {get;set;}

    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

最好的祝福!

Mar*_*zek 5

您的linq查询生成匿名对象列表(在select new { (...) }没有任何类型的情况下使用).你应该用它替换它select new Question() { (...) }.您应该根据Question类属性检查选择语法.

例如.当你有Name,Text并且Subject属性应该是这样的:

    var hej = from c in db.CoreValue
                join q in db.Question on c.CID equals q.QID
                join s in db.SubjectType on c.CID equals s.SID
                select new Question
                {
                    QID = q.QID,
                    QuestionText  = q.QuestionText
                };
Run Code Online (Sandbox Code Playgroud)