如何使用 C# 将 Include 动态添加到 ObjectSet<Entity>?
Posted
技术标签:
【中文标题】如何使用 C# 将 Include 动态添加到 ObjectSet<Entity>?【英文标题】:How can I dynamically add Include to ObjectSet<Entity> using C#? 【发布时间】:2013-04-09 21:00:56 【问题描述】:我想从输入 params[]
动态添加 Include
s。我该怎么做?
这是我的代码
IQueryable<Work> query = this.ObjectContext.Works
.Include("EmployeeSender.Person")
.Include("EmployeeReceiver.Person")
.Include("WorkCode")
.Include("WorkFlowStep.WorkFlowFormState")
.Include("WorkFlow")
.Include("WorkRoot.EmployeeSender.Person")
.Include("WorkParent");
【问题讨论】:
删除这个问题作为发布另一个 打扰一下,发送无意变成重复 是的,这就是我指出你的原因,我知道这是错误的,否则为什么会在几秒钟内发布双重问题。 LINQ query: Dynamically add Includes at run time 的可能重复项 【参考方案1】:在一个循环中,例如:
IQueryable<Work> query = null;
query = this.ObjectContext.Works;
foreach (var param in params)
query = query.Include(param);
var result = query.ToList();
正如 Christian Dietz 所提到的,您可以将其放入扩展方法中,使其可重用。
【讨论】:
在 query.Include(param);引发此错误。 “‘System.Linq.IQueryable您可以将L-Three的答案与以下问题中的扩展方法结合起来。
Using .Include() when joining a view using Entity Framework
public static IQueryable<T> Include<T>(this IQueryable<T> sequence, params string[] includes)
var objectQuery = sequence as ObjectQuery<T>;
if (objectQuery != null)
foreach(item in includes)
objectQuery.Include(item);
return objectQuery;
return sequence;
那么你应该可以使用 include like:
IQueryable<Work> query = null;
query = this.ObjectContext.Works.Include("Something", "Whatever");
【讨论】:
【参考方案3】:EF Core 尚无法进行延迟加载。 Refer here.
您也可以使用预先加载。
阅读此article
下面是我为实现预加载而创建的扩展方法。
扩展方法:
public static IQueryable<TEntity> IncludeMultiple<TEntity, TProperty>(
this IQueryable<TEntity> source,
List<Expression<Func<TEntity, TProperty>>> navigationPropertyPath) where TEntity : class
foreach (var navExpression in navigationPropertyPath)
source= source.Include(navExpression);
return source.AsQueryable();
存储库调用:
public async Task<TEntity> FindOne(ISpecification<TEntity> spec)
return await Task.Run(() => Context.Set<TEntity>().AsQueryable().IncludeMultiple(spec.IncludeExpression()).Where(spec.IsSatisfiedBy).FirstOrDefault());
用法:
List<object> nestedObjects = new List<object> new Rules();
ISpecification<Blog> blogSpec = new BlogSpec(blogId, nestedObjects);
var challenge = await this._blogRepository.FindOne(blogSpec);
依赖关系:
public class BlogSpec : SpecificationBase<Blog>
readonly int _blogId;
private readonly List<object> _nestedObjects;
public ChallengeSpec(int blogid, List<object> nestedObjects)
this._blogId = blogid;
_nestedObjects = nestedObjects;
public override Expression<Func<Challenge, bool>> SpecExpression
get return blogSpec => blogSpec.Id == this._blogId;
public override List<Expression<Func<Blog, object>>> IncludeExpression()
List<Expression<Func<Blog, object>>> tobeIncluded = new List<Expression<Func<Blog, object>>>();
if (_nestedObjects != null)
foreach (var nestedObject in _nestedObjects)
if (nestedObject is Rules)
Expression<Func<Blog, object>> expr = blog => blog.Rules;
tobeIncluded.Add(expr);
return tobeIncluded;
如果有帮助会很高兴。请注意,这不是生产就绪代码。
【讨论】:
以上是关于如何使用 C# 将 Include 动态添加到 ObjectSet<Entity>?的主要内容,如果未能解决你的问题,请参考以下文章