入れ子になったテーブルを使用する典型的なフォーラム アプリがあります。
フォーラム => トピック => 投稿
LINQ を使用して ViewModel を設定し、フォーラム内のトピック内の投稿の最後のポスターを表示しようとしていますが、クエリを実行するとエラーが発生します。
LINQ to Entities does not recognize the method 'centreforum.Models.Post LastOrDefault[Post](System.Collections.Generic.IEnumerable 1[centreforum.Models.Post]) method, and this method cannot be translated into a store expression
私はそれがクエリ内のこの行にあることを知っています:
LastPost = f.Topics.FirstOrDefault().Posts.LastOrDefault().Author
私のコントローラーは:
public ActionResult Index()
{
var forum = db.Fora.Include(x => x.Topics)
.Select(f => new ForumViewModel
{
ForumId =f.ForumId,
Title=f.Title,
Description=f.Description,
Topics=f.Topics.Count(),
Posts=f.Topics.FirstOrDefault().Posts.Count(),
LastPost = f.Topics.FirstOrDefault().Posts.LastOrDefault().Author
}
).ToList();
return View(forum);
}
私のモデルは次のとおりです。
namespace centreforum.Models
{
public class Forum
{
public int ForumId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public List<Topic> Topics { get; set; }
}
public class Topic
{
public int TopicId { get; set; }
public int ForumId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Author { get; set; }
public string DateOfPost { get; set; }
public int Views { get; set; }
public Forum Forum { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public int TopicId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Author { get; set; }
public string DateOfPost { get; set; }
public int MyProperty { get; set; }
public Topic Topic { get; set; }
}
}
...そして私のViewModelは:
namespace centreforum.Models
{
public class ForumViewModel
{
public int ForumId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Topics { get; set; }
public int Posts { get; set; }
public string LastPost { get; set; }
}
}
私のクエリ内で、トピック内の最後の投稿を見つけるのを手伝ってくれる人はいますか?
ありがとうございました、
マーク