如何在EF中动态包含?

时间:2018-08-05 13:47:18

标签: entity-framework

我想传递逗号分隔的字符串,然后迭代进行动态包含,但我不知道如何做。

public IEnumerable<Inventory> GetInventories(string includeProperties = "")
        {
           var query = AppContext.Inventories;
           //i'm thinking of doing this
           foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            return query.ToList();


           // I want to achieve the code below
            return AppContext.Inventories
                .Include(i => i.Location)
                .Include(i => i.Product)
                .ToList();
        }

query = query.Include(includeProperty);中存在错误“ 无法将DbQuery隐式转换为DbSet

我不知道如何将query.Include(includeProperty);转换为.Include(i => i.Location)

更新

query = query.Include(includeProperty);更改为query.Include(includeProperty);,然后命名为GetInventories("Location,Product"),但其中不包括地理位置和产品。

0 个答案:

没有答案