【发布时间】:2021-07-24 17:45:30
【问题描述】:
我在使用数据库方面有点老派,对 LINQ 不太熟悉。我找到了一个 LINQ 扩展来实现我所需要的,但我坚持正确使用它。我是GroupJoin
我真正需要做的是“每个帖子的类别和该类别的数量”
型号
发布内容
public class PostContent
{
[Key]
public Guid Id { get; set; }
public string title { get; set; }
public string subTitle { get; set; }
public string description { get; set; }
public DateTime postDate { get; set; } = DateTime.Now;
public string post { get; set; }
public Guid? postCatId { get; set; }
[ForeignKey("postCatId")]
public virtual PostCategory postCategory { get; set; }
}
帖子类别
public class PostCategory
{
[Key]
public Guid Id { get; set; }
public string categoryName { get; set; }
public virtual List<PostContent> postContents { get; set; }
}
我的尝试“但什么也没返回”
var NumberOfCategoriesForEachPost = context.postCategories
.GroupJoin( context.postContents,
cat => cat.Id,
con => con.postCatId,
(Category, Content) => new
{
Cate = Category.categoryName,
Cont = Content.Count()
});
【问题讨论】:
标签: sql-server asp.net-mvc entity-framework linq asp.net-core