我想在每封邮件中包含可选的List<Image>
对象。我宁愿不必每次对话都要去数据库。
我通常只选择对话并使用Include
来包含所需的属性,但该方法不支持嵌套集合上的Take
,这是我的用例的必要条件。
var conversations = await (from c in db.Conversations
select new {
conversation = c,
messages = c.Messages.OrderByDescending(m => m.CreateDate).Take(50)
}).ToListAsync();
现在消息没有获得相关的List<Image>
对象
答案 0 :(得分:2)
让您的messages
成为包含所需属性的投影:
var conversations = await (from c in db.Conversations
select new {
conversation = c,
messages = c.Messages.OrderByDescending(m => m.CreateDate)
.Select(m => new {message = m, relatedThing = m.RelatedThing})
.Take(50)
}).ToListAsync();