【问题标题】:C# Yield return list, not single object [duplicate]C#产量返回列表,而不是单个对象[重复]
【发布时间】:2019-03-17 18:46:51
【问题描述】:

我需要批量处理子列表来批量处理对象。 Parallel.ForEach 不可行,因为批处理发布到 HTTP 请求。

public IEnumerable<T> BatchProcess<T>(IEnumerable<T> list, int batchSize)
    {
        int page = 0;
        IEnumerable<T> batch;
        while ((batch = list.Skip(page).Take(batchSize)).Count() != 0)
        {
            page++;
            yield return batch; //Error CS0029  Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'T'
        }

    }

错误 CS0029 无法将类型“System.Collections.Generic.List”隐式转换为“T”。

期望“yield return”返回一个对象列表,而不是返回单个对象。

【问题讨论】:

标签: c# yield-return


【解决方案1】:

你的方法签名应该是

public IEnumerable<IEnumerable<T>> BatchProcess<T>(IEnumerable<T> list, int batchSize)

【讨论】:

  • 页面上也应该有增量操作
猜你喜欢
  • 2021-09-18
  • 2015-03-25
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2020-07-17
相关资源
最近更新 更多