分层数据的 ABP AppService
ABP AppService for hierarchical data
我需要一个 AppService 来加载带有实体递归集合的树视图:
===Products===
Id
Description
Price
Products[] ====> Id
Description
Price
Products[]
====> Id
Description
Price
Products[] ====> Id
Description
Price
Products[]
有现成的class可以推导吗?如果没有,您能否建议 class 派生什么或实现什么接口,以及如何进行?
PS: 可能有完整的 CRUD 操作,但最重要的是了解如何加载数据。
您可以使用Include
方法来指定要包含在查询结果中的相关数据。
您可以参考下面的示例。
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace EFQuerying.RelatedData
{
public class Sample
{
public static void Run()
{
#region SingleInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ToList();
}
#endregion
#region IgnoredInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.Select(blog => new
{
Id = blog.BlogId,
Url = blog.Url
})
.ToList();
}
#endregion
#region MultipleIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.Include(blog => blog.Owner)
.ToList();
}
#endregion
#region SingleThenInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ToList();
}
#endregion
#region MultipleThenIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.ToList();
}
#endregion
#region MultipleLeafIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.Include(blog => blog.Posts)
.ThenInclude(post => post.Tags)
.ToList();
}
#endregion
#region IncludeTree
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.Include(blog => blog.Owner)
.ThenInclude(owner => owner.Photo)
.ToList();
}
#endregion
#region Eager
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
context.Entry(blog)
.Collection(b => b.Posts)
.Load();
context.Entry(blog)
.Reference(b => b.Owner)
.Load();
}
#endregion
#region NavQueryAggregate
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
var postCount = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Count();
}
#endregion
#region NavQueryFiltered
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
var goodPosts = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Where(p => p.Rating > 3)
.ToList();
}
#endregion
}
}
}
_productRepository
.GetAllIncluding(x=>x.Products)
.Where(p => p.Id == 1); // 或者任何条件
.ToList()
这将为您提供 ID 为 1 的产品和子产品的列表。
我需要一个 AppService 来加载带有实体递归集合的树视图:
===Products===
Id
Description
Price
Products[] ====> Id
Description
Price
Products[]
====> Id
Description
Price
Products[] ====> Id
Description
Price
Products[]
有现成的class可以推导吗?如果没有,您能否建议 class 派生什么或实现什么接口,以及如何进行?
PS: 可能有完整的 CRUD 操作,但最重要的是了解如何加载数据。
您可以使用Include
方法来指定要包含在查询结果中的相关数据。
您可以参考下面的示例。
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace EFQuerying.RelatedData
{
public class Sample
{
public static void Run()
{
#region SingleInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ToList();
}
#endregion
#region IgnoredInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.Select(blog => new
{
Id = blog.BlogId,
Url = blog.Url
})
.ToList();
}
#endregion
#region MultipleIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.Include(blog => blog.Owner)
.ToList();
}
#endregion
#region SingleThenInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ToList();
}
#endregion
#region MultipleThenIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.ToList();
}
#endregion
#region MultipleLeafIncludes
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.Include(blog => blog.Posts)
.ThenInclude(post => post.Tags)
.ToList();
}
#endregion
#region IncludeTree
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.Include(blog => blog.Owner)
.ThenInclude(owner => owner.Photo)
.ToList();
}
#endregion
#region Eager
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
context.Entry(blog)
.Collection(b => b.Posts)
.Load();
context.Entry(blog)
.Reference(b => b.Owner)
.Load();
}
#endregion
#region NavQueryAggregate
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
var postCount = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Count();
}
#endregion
#region NavQueryFiltered
using (var context = new BloggingContext())
{
var blog = context.Blogs
.Single(b => b.BlogId == 1);
var goodPosts = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Where(p => p.Rating > 3)
.ToList();
}
#endregion
}
}
}
_productRepository .GetAllIncluding(x=>x.Products) .Where(p => p.Id == 1); // 或者任何条件 .ToList()
这将为您提供 ID 为 1 的产品和子产品的列表。