cannot be called with instance of type 'System

2020-05-05 18:05发布

I want to Find Username by userId

this code snippet working

Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName,

and this once not working in following controller class

Comment_CreateBy = db.AspNetUsers.Find(c.CreatedBy).UserName,

this is my model classes

public class DiscussionVM
{
    public int Disussion_ID { get; set; }
    public string Discussion_Title { get; set; }
    public string Discussion_Description { get; set; }
    public Nullable<System.DateTime> Discussion_CreateDate { get; set; }
    public string Discussion_CreateBy { get; set; }

    public string Comment_User { get; set; }

    public IEnumerable<CommentVM> Comments { get; set; }

}

public class CommentVM
{

    public int Comment_ID { get; set; }
    public Nullable<System.DateTime> Comment_CreateDate { get; set; }
    public string Comment_CreateBy { get; set; }
    public string Comment_Description { get; set; }

}

this is whole controller class

    public ActionResult Discussion_Preview()
    {
        int Discussion_ID = 1;

        var discussion = db.AB_Discussion.Where(d => d.Discussion_ID == Discussion_ID).FirstOrDefault();
        var comments = db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID);



        DiscussionVM model = new DiscussionVM()
        {

            Disussion_ID = discussion.Discussion_ID,
            Discussion_Title = discussion.Discussion_Name,
            Discussion_Description = discussion.Discussion_Name,
            Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName,
            Discussion_CreateDate = discussion.CreatedDate,

            Comments = comments.Select(c => new CommentVM()

            {
                Comment_ID = c.Comment_ID,
                Comment_Description = c.Comment_Discription,
                Comment_CreateBy = db.AspNetUsers.Find(c.CreatedBy).UserName,                    
                Comment_CreateDate = c.CreatedDate

            })

        };



        return View(model);
    }

Getting following error

Method 'Project.Models.AspNetUser Find(System.Object[])' declared on type 'System.Data.Entity.DbSet1[Project.Models.AspNetUser]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery1[Project.Models.AspNetUser]'

1条回答
够拽才男人
2楼-- · 2020-05-05 18:51
Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName

Works because discussion is an in-memory object because you are executing a query by calling FirstOrDefault on it:

var discussion = db.AB_Discussion.Where(d => d.Discussion_ID == Discussion_ID).FirstOrDefault();

On the other hand in the following statement:

db.AspNetUsers.Find(c.CreatedBy).UserName

c is not queried yet because

db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID)

returns an IQueriable and not the actual collection of comments

The easiest way to fix it is to bring all your comments into memory (since you are anyway need them all) :

var comments = db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID).ToList();
查看更多
登录 后发表回答