cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery

后端 未结 1 1190
我在风中等你
我在风中等你 2021-01-24 05:36

I want to Find Username by userId

this code snippet working

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

1条回答
  •  生来不讨喜
    2021-01-24 06:14

    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();
    

    0 讨论(0)
提交回复
热议问题