我是 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,
….
資料庫圖影像
模型類:
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)
{....}
}
uj5u.com熱心網友回復:
正如我所懷疑的.Select,.Include通過從_context.Articles.
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 中添加的多對多關系(也稱為跳過導航),這會稍微簡單一些。因為您有一個可以直接分配的Authorand集合的導航屬性。Tag
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/428261.html
上一篇:資料庫連接在哪里
下一篇:尋找更好的方法來擴展方法
