分组两个表EF核心聚合
我想创建一条有关主题统计数据的路线,因此我尝试使用Join和Groupby来实现它,我希望Result display exparyID,标题,身体和总主题每个类别,但我的实现遇到了这个错误。
.groupby(x => x.title)'无法翻译。可以通过插入“可忽略的”,“ asasyncenumerable”,“ tolist”或“ tolistAsync'
'来重写可以翻译的形式的查询,或者明确切换到客户端评估。
。
public class Topic
{
public int Id { get; set; }
public string TItle { get; set; }
public string body { get; set; }
public int CategoryId { get; set; }
[JsonIgnore]
public Category Category { get; set; }
}
public class Category
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
}
[HttpGet("stats")]
public async Task<ActionResult> getTopicStats()
{
var stats = _context.Topics.Join(
_context.Categories,
topic => topic.CategoryId,
cat => cat.Id,
(topic, cat) => new
{
catId = topic.CategoryId,
desc = cat.Description,
title = cat.Title
}).GroupBy(x => x.title);
await stats.ToListAsync();
Console.WriteLine(stats);
}
I want to create a route that gets the stats about topics, so I tried using join and groupBy to achieve it, I want the result display categoryId, title, body and total topics per category but I got this error with my implementation.
.GroupBy(x => x.title)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'
Please does anyone know how to solve this?
public class Topic
{
public int Id { get; set; }
public string TItle { get; set; }
public string body { get; set; }
public int CategoryId { get; set; }
[JsonIgnore]
public Category Category { get; set; }
}
public class Category
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
}
[HttpGet("stats")]
public async Task<ActionResult> getTopicStats()
{
var stats = _context.Topics.Join(
_context.Categories,
topic => topic.CategoryId,
cat => cat.Id,
(topic, cat) => new
{
catId = topic.CategoryId,
desc = cat.Description,
title = cat.Title
}).GroupBy(x => x.title);
await stats.ToListAsync();
Console.WriteLine(stats);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要加入linq到实体。只需遵循您的导航属性即可。
,以便使用适当的导航属性,
这是类似的:
Don't join in LINQ-to-Entities. Just follow your Navigation Properties.
So with proper Navigation properties,
this is something like: