不能使用类型为'System.Data.Entity.Core.Objects.ObjectQuery的实例进行调用 [英] cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery
问题描述
我想通过userId查找用户名
I want to Find Username by userId
此代码段有效
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,
这是我的模型课
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; }
}
这是整个控制器类
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);
}
出现以下错误
在类型'System.Data.Entity.DbSet
1[Project.Models.AspNetUser]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery
1 [Project.Models.AspNetUser]'上声明的方法'Project.Models.AspNetUser Find(System.Object [])'
Method 'Project.Models.AspNetUser Find(System.Object[])' declared on type 'System.Data.Entity.DbSet
1[Project.Models.AspNetUser]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery
1[Project.Models.AspNetUser]'
推荐答案
Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName
之所以行之有效,是因为讨论是一个内存对象,因为您正在通过在其上调用FirstOrDefault
来执行查询:
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();
另一方面,以下语句:
db.AspNetUsers.Find(c.CreatedBy).UserName
c
,因为
db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID)
返回一个IQueriable
而不是评论的实际集合
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();
这篇关于不能使用类型为'System.Data.Entity.Core.Objects.ObjectQuery的实例进行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!