【问题标题】:How can I get the following items from a list with entity framework?如何从具有实体框架的列表中获取以下项目?
【发布时间】:2019-12-28 19:08:29
【问题描述】:

我有一个非常大的列表,我想从 50 个项目中提取部分内容,我将针对这些项目实施以下操作:

try
{
    using (var context = new ccoFinalEntities())
    {
        return context.sales
            .Where(p => true == p.status && myID == p.id)
            .Take(50)
            .ToList();
    }
}
catch
{
    return null;
}

我认为这将返回前 50 件商品,我的问题是:

我怎样才能得到下一个 50 等等直到完全提取列表?

欢迎任何cmets或建议

【问题讨论】:

  • 第一次调用return,您的方法将退出,您需要再次调用该方法。
  • 使用 Select(x,i) => 其中 'i' 是索引。然后使用 GroupBy i/50 获取 50 组。

标签: c# list entity-framework take


【解决方案1】:
public List<Item> GetDate(int page, int size = 50)
    {
    try
    {
      using (var context = new ccoFinalEntities())
      {
        return context.sales.Where(p => true == p.status && myID == p.id)
                            .Skip(page * size).Take(size).ToList();
      }
    }
    catch
    {
      return null;
    }
}

然后调用:

GetData(0);
GetData(1);
GetData(2);
GetData(3);

.....

Skip 首先忽略 page * size (1 * 50, 2 * 50, ...) 项和 Take size (50) 项

【讨论】:

  • "操作无法完成,因为 DbContext 已被释放。"
  • @FabiánRomo 不可能。确保在处理之前调用ToList(),并确保您渴望加载引用属性。
  • "方法 'Skip' 仅支持 LINQ to Entites 中的排序输入。必须在方法 'Skip' 之前调用方法 'OrderBy'"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 2021-10-17
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
相关资源
最近更新 更多