我正在使用新的 核心和 EF.
I am using new core and EF.
我需要有关 include linq 命令的帮助.我有一些 1:N 模型,如果集合包含一些标记为已删除的数据,我不想包含它们.
I need help with include linq command. I have some 1:N models and if the collection contais some data marked like deleted I do not want to include them.
怎么做?
var company = await _context.Company .Include(y => y.Administrators) .Include(y => y.CompanyPartTimers) .Include(z => z.WorkPlaces) .Include(z => z.Requirements) .FirstAsync(x => x.Id == id);如果我添加条件
.Include(z => z.WorkPlaces).Where(x=>x.WorkPlaces.Where(x=>!x.IsDeleted))它不起作用.如何正确书写?
It doesn't work. How to write this correctly?
接下来是我有 IDeletable 接口,如果我有一些自定义的 linq 表达式并且可以为 ex 做会更好.
Next thing is I have IDeletable Interface and it would be better if I had some custom linq expression and could do for ex.
.Include(z => z.WorkPlaces).GetNonDeleted()有人知道怎么做吗?我试过这样的事情
Does anyone know how to do it? I tryed something like this
public static class LinqExtension { public static IEnumerable<T> GetActive<T>(this IEnumerable<T> source) where T : class, IDeletable { return source.Where(x => x.IsDeleted); } }谢谢各位.
推荐答案您可以在 DbContext 中配置查询过滤器.
You can configure a Query Filter in your DbContext.
modelBuilder.Entity<Administrator>() .HasQueryFilter(admin => !EF.Property<boolean>(admin, "IsDeleted"));应该做的伎俩
参考链接:docs.microsoft/en-us/ef/core/querying/filters
更多推荐
.NET 核心 2 EF 包括
发布评论