分组两个表EF核心聚合

发布于 2025-01-31 12:55:36 字数 1259 浏览 3 评论 0原文

我想创建一条有关主题统计数据的路线,因此我尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2025-02-07 12:55:36

不要加入linq到实体。只需遵循您的导航属性即可。

想要每个类别的结果显示类别,标题,身体和总主题

,以便使用适当的导航属性,

public class Category
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    public ICollection<Topic> Topics {get; set;} = new HashSet<Topic>();
}

这是类似的:

from c in db.Categories
select new 
{ 
  c.CategoryId,
  CategoryTitle = c.Title,
  CategoryDescription = c.Description,
  TopicCount = c.Topics.Count() 
};

Don't join in LINQ-to-Entities. Just follow your Navigation Properties.

want the result display categoryId, title, body and total topics per category

So with proper Navigation properties,

public class Category
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    public ICollection<Topic> Topics {get; set;} = new HashSet<Topic>();
}

this is something like:

from c in db.Categories
select new 
{ 
  c.CategoryId,
  CategoryTitle = c.Title,
  CategoryDescription = c.Description,
  TopicCount = c.Topics.Count() 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文