在使用linq to sql的mvc应用程序中,用户代码未处理NullReferenceException

本文关键字:用户 代码 NullReferenceException 未处理 应用程序 mvc linq to sql | 更新日期: 2023-09-27 17:50:27

我在控制器中调用下面的方法。

 var comments = _blogService.GetBlogComments(bc.CommentParentID);//Controller code
类代码:

public virtual IList<BlogComment> GetBlogComments(int CommentParentId)
        {
            if (CommentParentId != 0)
            {
                var query = from bc in _blogCommentRepository.Table
                            where bc.CommentParentID == CommentParentId
                            select bc;
                var comments = query.ToList();
                return comments;
            }
            else
                return null;
        }

我在

上得到错误
var query = from bc in _blogCommentRepository.Table
                            where bc.CommentParentID == CommentParentId
                            select bc;][1]
https://i.stack.imgur.com/uMWcV.jpg

在使用linq to sql的mvc应用程序中,用户代码未处理NullReferenceException

绑定的第一件事是您在GetBlogComments方法中使用的_blogCommentRepository变量是null。因此,请确保在使用该变量之前对其进行初始化:

_blogCommentRepository = new ...