ModelFactoryクラスのメソッドを使用して、Object Initializerを使用する代わりにCommentWithUserDetailsのインスタンスを作成します。これはできますか?エンティティへFactoryメソッドをLINQのresultSelectorとして使用する方法Join
ExceptionMessage
LINQは方法を認識しない 方法 'WebApi.Models.CommentWithUserDetailsは(WebApi.Models.Comment、 WebApi.Models.ApplicationUser)を作成'、そしてこの方法は することはできませんストア式に変換されます。
public IEnumerable<CommentWithUserDetails> GetAllPostComments(int postId)
{
var commentsWithUserDetails = _context.Comments.Join(_context.Users,
c => c.UserId,
u => u.Id,
(comment, user) => _modelFactory.Create(comment, user));
return commentsWithUserDetails;
}
public class ModelFactory
{
public CommentWithUserDetails Create(Comment comment, ApplicationUser user)
{
return new CommentWithUserDetails
{
Id = comment.Id,
PostId = comment.PostId,
Body = comment.Body,
Name = user.Name
};
}
}