- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为 Entity Framework Core 2.0 编写一个存储库方法,该方法可以使用 .ThenInclude 处理返回的子属性集合,但我在使用第二个表达式时遇到了问题。这是 .Include 的工作方法,它将返回实体的子属性(您提供 lambda 列表)。
public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.Where(predicate).FirstOrDefault();
}
现在,我尝试编写一种方法,该方法将接受两个表达式的元组并将它们提供给 .Include(a => a.someChild).ThenInclude(b => b.aChildOfSomeChild) 链。这不是一个完美的解决方案,因为它只处理一个 child 的一个 child ,但这是一个开始。
public T GetSingle(Expression<Func<T, bool>> predicate, params Tuple<Expression<Func<T, object>>, Expression<Func<T, object>>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty.Item1).ThenInclude(includeProperty.Item2);
}
return query.Where(predicate).FirstOrDefault();
}
Intellisense 返回错误“无法从用法中推断出类型,请尝试明确指定类型”。我有一种感觉,因为 Item2 中的表达式需要归类为与 Item1 有某种关联,因为它需要知道它具有的子关系。
编写这样的方法有什么想法或更好的技术吗?
最佳答案
我在网上找到了这个存储库方法,它完全符合我的要求。 Yared 的回答很好,但并非一直如此。
/// <summary>
/// Gets the first or default entity based on a predicate, orderby delegate and include delegate. This method default no-tracking query.
/// </summary>
/// <param name="selector">The selector for projection.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="orderBy">A function to order elements.</param>
/// <param name="include">A function to include navigation properties</param>
/// <param name="disableTracking"><c>True</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
/// <remarks>This method default no-tracking query.</remarks>
public TResult GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector,
Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true)
{
IQueryable<TEntity> query = _dbSet;
if (disableTracking)
{
query = query.AsNoTracking();
}
if (include != null)
{
query = include(query);
}
if (predicate != null)
{
query = query.Where(predicate);
}
if (orderBy != null)
{
return orderBy(query).Select(selector).FirstOrDefault();
}
else
{
return query.Select(selector).FirstOrDefault();
}
}
用法:
var affiliate = await affiliateRepository.GetFirstOrDefaultAsync(
predicate: b => b.Id == id,
include: source => source
.Include(a => a.Branches)
.ThenInclude(a => a.Emails)
.Include(a => a.Branches)
.ThenInclude(a => a.Phones));
关于c# - 如何在 EF Core 2 中为 .ThenInclude 编写 Repository 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374252/
众所周知,EF-Core 中没有延迟加载。因此,这意味着我不得不事后才进行查询。既然要思考,那我就试着好好的去做吧。 我有一个相当标准的更新查询,但我想嘿,我不必总是包含 HeaderImage 和
我一直在尝试使用 Entity Framework,在遇到以下错误后,我尝试使用 ThenInclude 来解决它。 The expression '[x].ModelA.ModelB' passed
这是我的通用存储库: public class Repository : IRepository where T : BaseEntity { private DbContext _dbCon
我将.NET Framework(EF6)代码传输到ASP.NET Core(EF Core),但我偶然发现了这个问题。这是一些示例代码: 在EF6中,我使用Include()和Select()进行预
有没有人看到我做错了什么? ProjectActivityTasks 具有 UnitOfMeasureId 和 ProjectActivityTaskTypeId。按照它的编写方式,它认为 UnitO
我看到一些包含 ThenInclude 的 LINQ 代码示例。我在哪里可以找到这样的方法?在外部库或其他命名空间中。 我想用这样的方法获取派生数据,但是我找不到这样的方法或者你知道其他解决方案吗
这个问题在这里已经有了答案: Filtering on Include in EF Core (9 个回答) 关闭 5 个月前。 我有 3 个实体: Questionnaire.cs: public
我使用以下查询根据需要过滤我的 BranchId。 var query = await _dbContext.TargetItems .Include(i => i.Br
我有一个这样的查询 return await _ctx.Activities .Include(a => a.Attributes) .Include(
我的 IQueryable 看起来像这样: IQueryable query = context.Set(); query = query.Include("Car").ThenInclude("M
英孚核心 3.1 我见过Specification例如,并希望实现 ThenInclude 模式。 public static class QuerySpecificationExtensions {
我正在创建一个 ASP.NET Core API 应用程序,并依赖于 EF Core。我有这样定义的实体: public class AppUser : IdentityUser { publ
我将 Entity Framework Core 与 Repository Pattern 一起使用,但遇到一个问题。 我有类 Customer、Company 和 Email,其中隐藏了与此处无关的
假设首先有这些模型: Method 有一个 OriginalCodeOriginalCode 有很多 MutantMutant 有许多 ParseSubTree 现在,当查询 Method 时,我希望
这个问题在这里已经有了答案: Can not load related data with Include or ThenInclude or Select/Many with ONE query
我需要在 ThenInclude 中使用 where var templatesFields = await _context.Sections .Include(x
假设首先拥有这些模型: Method有一个 OriginalCodeOriginalCode有很多Mutant秒Mutant有很多ParseSubTree秒 现在在 Method 上查询时我想要另一个
以前(使用 .net 4.5.2 和 EF 6 时)。我有一个通用的 Get接受多个包含的方法如下; public abstract class DataContext : IdentityDbCon
好的,所以我有一个表 Building,其中包含该建筑物中的所有人员。然而,每个人都有一个职业,它本身就是一个实体,根据可用的内容添加到 Person 实体。 var data = _dbcontex
我正在使用 EF core 2.0 并想过滤子集合。谁能帮助我如何在 EF 核心 2.0 中执行此操作? var items = await _context.RiskType
我是一名优秀的程序员,十分优秀!