7

我想Include从 input 动态添加 s params[]。我怎样才能做到这一点?

这是我的代码

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");
4

3 回答 3

8

在一个循环中,例如:

IQueryable<Work> query = null;  

query = this.ObjectContext.Works;
foreach (var param in params)
{
    query = query.Include(param);
}
var result = query.ToList();

正如 Christian Dietz 所提到的,您可以将其放入扩展方法中,使其可重用。

于 2013-04-17T07:45:27.290 回答
3

您可以将 L-Three 的答案与以下问题中的扩展方法结合起来。

使用 Entity Framework 加入视图时使用 .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;
}

然后你应该能够使用包括:

IQueryable<Work> query = null;  

query = this.ObjectContext.Works.Include("Something", "Whatever");
于 2013-04-17T07:53:40.120 回答
1

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

如果有帮助会很高兴。请注意,这不是生产就绪代码。

于 2017-07-12T13:33:41.257 回答