繁体   English   中英

具有相同值的分组联合表(文章)

[英]Group joint table with same value (article)

我是 C#.NET 的新手。 我正在尝试按文章对我的 GET 请求的结果进行分组,当我尝试使用 groupby 时,它显示错误。

这是我没有分组的原始代码。

//GET api/Articles 
[HttpGet] 
public async Task<ActionResult<IEnumerable<AuthorTagArticle>>> GetArticles() {
        return await _context.AuthorArticles.Join(
            _context.TagArticles,
            article => article.ArticleId,
            tag => tag.ArticleId,
            (article, tag) => new AuthorTagArticle
            {
                Article = article.Article,
                Author = article.Author,
                Tag = tag.Tag
            })
            .ToListAsync(); }

错误:

获取文章 API 代码图片

结果:

未分组图像的结果

我想做这样的事情......

{
"article": {
    "articleId": 1,
    "articleTitle": "Article1",
    "articleContent": null,
    "createdOn": "0001-01-01T00:00:00"
},
"author": [{
    "userId": 1,
    "userName": "User 1",
    "userRole": "STAFF"
},{
    "userId": 2,
    "userName": "User 2",
    "userRole": "STAFF"
},{
    "userId": 3,
    "userName": "User 3",
    "userRole": "STAFF"
}]
"tag": [{
    "tagId": 2,
    "tagName": "Breaking News",
    "tagColorCode": "#FF0000"
},{
    "tagId": 7,
    "tagName": "Economics",
    "tagColorCode": "#7100FF"
}]
},{
"article": {
    "articleId": 2,
   ….

数据库图图像

楷模:

public class AuthorTagArticle
{ 
    public Article Article { get; set; }

    public User Author { get; set; }

    public Tag Tag { get; set; }
}

public class AuthorArticle
{
    [Key]
    [Column(Order=1)]
    public int ArticleId { get; set; }

    [Key]
    [Column(Order = 2)]
    public int AuthorId { get; set; }

    public virtual Article Article { get; set; }
    public virtual User Author { get; set; }
}

public class TagArticle
{
    [Key]
    [Column(Order = 1)]
    public int ArticleId { get; set; }

    [Key]
    [Column(Order = 2)]
    public int TagId { get; set; }

    public virtual Article Article { get; set; }

    public virtual Tag Tag { get; set; }
}

public class Article
{
    public int ArticleId { get; set; }

    public string ArticleTitle { get; set; }

    public string ArticleContent { get; set; }

    public DateTime CreatedOn { get; set; }

    [JsonIgnore] 
    public virtual ICollection<AuthorArticle> AuthorArticles { get; set; }
    [JsonIgnore]
    public virtual ICollection<TagArticle> TagArticles { get; set; }
}

public class Tag
{
    public int TagId { get; set; }

    public string TagName { get; set; }

    public string TagColorCode { get; set; }
    [JsonIgnore]
    public virtual ICollection<TagArticle> TagArticles { get; set; }
}

public class User
{        
    public int userId { get; set; }

    public string userName { get; set; }

    public string userRole { get; set; }

    [JsonIgnore]
    public virtual ICollection<AuthorArticle> AuthorArticles { get; set; }
}
 public class NewsContext : DbContext
    {
        public NewsContext(DbContextOptions<NewsContext> options) : base(options) {}

        public DbSet<Article> Articles { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<AuthorArticle> AuthorArticles { get; set; }

        public DbSet<TagArticle> TagArticles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {....}
    }

正如我所怀疑的,您拥有所有需要的多对多导航,只需从_context.Articles开始查询即可.Select.Include一切。

await _context.Articles
    .Where(...)
    .Select(article => new AuthorTagArticle
        {
            Article = article,
            Author = article.AuthorArticles.Select(a => a.Article).ToList(),
            Tag = article.TagArticle.Select(t => t.Tag).ToList()
        })
    .ToListAsync();

如果您使用 EF Core 5 中添加的多对多关系(也称为跳过导航),这会稍微简单一些。因为您有一个可以直接分配的AuthorTag collections 的导航属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM