具有相同值的组关节 table(文章)
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();
}
这是我得到的错误:
结果:
我会做这样的事情....
{
"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,
….
Database Diagram Image
型号类:
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
.[=17 开始查询,您拥有所有 many-to-many 导航,只需 .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 中添加的 many-to-many 关系(也称为跳过导航),这会稍微简单一些。因为您有 [=14= 的导航 属性 ] 和 Tag
个您可以直接分配的集合。
我是 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();
}
这是我得到的错误:
结果:
我会做这样的事情....
{
"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,
….
Database Diagram Image
型号类:
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
.[=17 开始查询,您拥有所有 many-to-many 导航,只需 .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 中添加的 many-to-many 关系(也称为跳过导航),这会稍微简单一些。因为您有 [=14= 的导航 属性 ] 和 Tag
个您可以直接分配的集合。