【问题标题】:How can I dynamically add Include to ObjectSet<Entity> using C#?如何使用 C# 将 Include 动态添加到 ObjectSet<Entity>?
【发布时间】:2013-04-09 21:00:56
【问题描述】:

我想从输入 params[] 动态添加 Includes。我该怎么做?

这是我的代码

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

【问题讨论】:

  • 删除这个问题作为发布另一个
  • 打扰一下,发送无意变成重复
  • 是的,这就是我指出你的原因,我知道这是错误的,否则为什么会在几秒钟内发布双重问题。

标签: c# entity-framework


【解决方案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’不包含‘Include’的定义,也没有接受‘System.Linq.IQueryable' 可以找到(您是否缺少 using 指令或程序集引用?)"
  • Include 是在 System.Data.Entity.DbExtensions 中声明的扩展方法。您可能缺少 System.Data.Entity 命名空间的 using 子句。
  • 谢谢 L-Tree,但我的项目是 asp.net 和 Silverlight 。可能这导致 System.Linq.IQueryable 不同。
  • Include 定义在 System.Data.Objects 命名空间里面 System.Data.Entity.dll
【解决方案2】:

您可以将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;
            }
    

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

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多