我如何做。包括在IEnumerable上工作 [英] How do i make .Include to work on an IEnumerable

查看:94
本文介绍了我如何做。包括在IEnumerable上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的存储库中运行查询。我必须通过ID获取产品,并将其显示在产品图像旁边的编辑视图中。



我的ProductRepository中有一个实现Get()的方法,即获取名称所有的产品和GetByID。我实现了一个通用存储库模式,其中包含一个工作类单元,如下所示

  public class GenericRepository< TEntity>其中TEntity:class 
{
internal SchoolContext context;
内部DbSet< TEntity> dbSet;

public GenericRepository(SchoolContext context)
{
this.context = context;
this.dbSet = context.Set< TEntity>();
}

public virtual IEnumerable< TEntity> Get(
Expression< Func< TEntity,bool>> filter = null,
Func< IQueryable< TEntity> IOrderedQueryable< TEntity>> orderBy = null,
string includeProperties = )
{
IQueryable< TEntity> query = dbSet;

if(filter!= null)
{
query = query.Where(filter);
}

foreach(var includeProperty in includeProperties.Split
(new char [] {','},StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}

if(orderBy!= null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}

public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}

我认为这是唯一的相关代码块。当我尝试运行我在教程中找到的查询的问题出现在我的下面的查询下面的图像中。

  Product product = db.Products.Include(s => s.Files).SingleOrDefault(s => s.ID == id); 

我不能使用db.Products,因为我正在使用一个单位的工作类,所以我必须运行查询与 _unit.ProductRepository.GetByID()。Include(s => s.Files).SingleOrDefault(s => s.ID == id); / p>

但是这似乎不可能,我被卡住了。

解决方案

您不能使用包含IEnumerable,它只适用于IQueryable,当您在存储库中调用 query.ToList(); 时,您的查询从数据库检索到内存在IEnumerable中,当您的数据在内存中时,Include不起作用。



您可以将要包含在查询中的对象作为参数传递给您您可以使用Get方法中的过滤器或订单。



您可以覆盖您的ProductRepository方法

 code> public override Product GetByID(object ID)
{
return db.Products.Include(p => p.Files).SingleOrDefaul t(p => p.ID == ID);
}

或者您不想永远返回文件

  public override Product GetByID(object ID,List< string> includes)
{

var query = db .Products.AsQueryable();
foreach(string include in includes)
{
query = query.Include(include);
}

return query.SingleOrDefault(p => p.ID == ID);
}

并调用像

  Product product = new ProductRepository()。GetByID(IDProduct,new List< string>(){Files}); 


I'm having trouble running a query on my repository. I have to fetch a product by the id and display it in an edit view alongside the image of the product.

There is a method in my ProductRepository that implements Get() i.e fetch all the product and GetByID as the name implies. I implemented a generic repository pattern with a unit of work class like below

public class GenericRepository<TEntity> where TEntity : class
    {
        internal SchoolContext context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(SchoolContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

I think thats the only relevant code block. The problem arises when i try to run a query that i found in a tutorial to fetch the product alongside the image with the query below

Product product = db.Products.Include(s => s.Files).SingleOrDefault(s => s.ID == id);

I cant use db.Products because i'm using a unit of work class so i have to run the query with _unit.ProductRepository.GetByID().Include(s => s.Files).SingleOrDefault(s => s.ID == id);

However this does not seem to be possible and i'm stucked.

解决方案

You can not use Include with a IEnumerable, It only works with IQueryable, when you invoke in your repository query.ToList(); you query is retrieved from database to memory in a IEnumerable and when your data is in memory Include doesn't work.

You can pass the objects that you want to include in your query as a parameter like you do with filter or order in your Get method.

You can override your ProductRepository method

    public override Product GetByID(object ID)
    {
        return db.Products.Include(p => p.Files).SingleOrDefault(p => p.ID == ID);
    }

or if you don't want always to return Files

    public override Product GetByID(object ID, List<string> includes)
    {

        var query = db.Products.AsQueryable();
        foreach (string include in includes)
        {
            query = query.Include(include);
       }

       return query.SingleOrDefault(p => p.ID == ID);
    }

And invoke like

 Product product = new ProductRepository().GetByID(IDProduct, new List<string>() { "Files" });

这篇关于我如何做。包括在IEnumerable上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆