在我的代码中需要使用IEnumerable<>几次因此得到Resharper错误"可能的多个枚举IEnumerable".
示例代码:
public List<object> Foo(IEnumerable<object> objects)
{
if (objects == null || !objects.Any())
throw new ArgumentException();
var firstObject = objects.First();
var list = DoSomeThing(firstObject);
var secondList = DoSomeThingElse(objects);
list.AddRange(secondList);
return list;
}
Run Code Online (Sandbox Code Playgroud)
objects参数List,然后避免可能的多次枚举,但后来我没有得到我能处理的最高对象. IEnumerable到List在方法的开头: public List<object> Foo(IEnumerable<object> objects)
{
var objectList = objects.ToList();
// ...
}
Run Code Online (Sandbox Code Playgroud)
但这只是尴尬.
在这种情况下你会做什么?
在将其标记为重复之前,我已经看到了许多答案,例如“ 将IEnumerable转换为DataTable”,并试图通过创建扩展方法的方式进行类似的操作。我问我的问题,因为问题可能在其他地方。
从本质上讲,到目前为止,我有相当大的IEnumerable(大约16-17百万个项目),在使用扩展方法尝试转换为数据表之前,我并没有遇到任何问题:
/// <summary>
/// Converts IEnumberable to datatable. Mainly for use when using SQLBulkCopy/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="customColumnOrder">Custom order for columns allows me to make sure that the order of columns will always be the same. Am open for suggestions for better ways to do this</param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> collection, List<Tuple<string, int, int>> customColumnOrder)
{
DataTable dt = new DataTable();
var type = collection.First().GetType();
foreach (var …Run Code Online (Sandbox Code Playgroud)