如何动态地将Include添加到ObjectSet使用c#

本文关键字:ObjectSet Entity 使用 添加 Include 何动态 动态 | 更新日期: 2023-09-27 18:09:30

我想从输入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");

如何动态地将Include添加到ObjectSet<Entity>使用c#

在循环中,例如:

IQueryable<Work> query = null;  
query = this.ObjectContext.Works;
foreach (var param in params)
{
    query = query.Include(param);
}
var result = query.ToList();

正如Christian Dietz提到的,你可以把它放在一个扩展方法中,这样它就可以被重用了。

在下面的问题中,你可以将L-Three的答案与可拓法结合起来。

在使用实体框架加入视图时使用。include ()

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:

IQueryable<Work> query = null;  
query = this.ObjectContext.Works.Include("Something", "Whatever");

EF Core还不支持延迟加载。参考这里。

或者你可以使用即时加载。

阅读本文

下面是我创建的实现即时加载的扩展方法。

扩展方法:

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;
        }

如果有帮助我会很高兴。