我有一个包含 10000 个值的数组,我想从数组中取出每 50 个项目并执行一些字符串操作。我应该如何使用 LINQ 或 FOR 循环来做到这一点。提前致谢。
问问题
428 次
6 回答
2
您可以在循环中使用Skip
and ,如下所示:Take
for
for (int pos = 0 ; pos < values.Length ; pos += 50) {
var subArray = values.Skip(pos).Take(50).ToArray();
}
注意:如果values
不是数组或List<String>
.
但是,这不是最有效的方法:最好不要使用 LINQ,Array.Copy
而是使用方法:
var subArray = new String[50];
for (int pos = 0 ; pos < values.Length ; pos += 50) {
Array.Copy(values, pos, subArray, 0, 50);
}
上述解决方案要求它subArray
不存储为您计划进行的任何处理的一部分,并且values.Length
可以被 50 整除(它是 10000 个项目)。
于 2013-11-12T17:17:34.933 回答
1
你可以使用GetRange
List<string> elements = yourArray.ToList();
if (null != elements && elements.Count > 50)
{
for (int i = 0; i < elements.Count; i += 50)
{
Array result = elements.GetRange(i,50).ToArray();
// here you can pass the retrived list to your private method to do the necessary functionility.
StringOperationForArray(result);
}
}
于 2013-11-12T17:36:30.100 回答
0
于 2013-11-12T17:27:07.980 回答
0
数据实际上是如何枚举的?如果是这样,IEnumerable
那么即使是这样,也foreach
可以正常工作,否则List
即使是这样foreach
,或者IEnumerable.ForEach
也适用,即使对数据进行切片也不会对性能造成额外的损失。例如
foreach(var item in list)
{
//your operation here
}
或者,
list.ForEach(o=>
{
//your operation here
}
);
于 2013-11-15T09:24:29.980 回答
0
干得好:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
const int chunkSize = 50;
static void Main(string[] args)
{
var bigList = Enumerable.Range(0, 10000).Select(s => s.ToString()).ToList();
ChunkerProcess(bigList, i => { Console.WriteLine(i); });
}
private static void ChunkerProcess<T>(List<T> bigList, Action<T> action )
{
int pointer = 0;
List<T> chunks = bigList.GetRange(0, chunkSize>bigList.Count?bigList.Count:chunkSize);
while (chunks.Count > 0)
{
foreach (var chunk in chunks)
{
action.Invoke(chunk);
}
chunks.Clear();
if (chunkSize * pointer < bigList.Count)
{
chunks = bigList.GetRange(chunkSize * pointer, chunkSize * (pointer + 1) > bigList.Count ? bigList.Count - chunkSize * pointer : chunkSize);
pointer++;
}
}
}
}
}
于 2013-11-12T17:30:51.867 回答
0
如果您可以使用 Rx Extensions,这将是我最喜欢的解决方案。
namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
internal class Program
{
#region Constants
private const int chunkSize = 50;
#endregion
#region Methods
private static void Main(string[] args)
{
List<string> bigList = Enumerable.Range(0, 99).Select(s => s.ToString()).ToList();
bigList.ToObservable().Buffer(chunkSize).Subscribe(
chunk =>
{
foreach (string s in chunk)
{
Console.WriteLine(s);
}
Console.WriteLine(String.Empty.PadRight(80, '-'));
});
}
#endregion
}
}
结合并行扩展附加功能
namespace ConsoleApplication2
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Schedulers;
internal class Program
{
#region Constants
private const int ChunkSize = 50;
#endregion
#region Methods
private static void Main(string[] args)
{
var cancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;
var workStealingTaskScheduler = new WorkStealingTaskScheduler(Environment.ProcessorCount - 1);
var bag = new ConcurrentBag<Task>();
IEnumerable<string> bigList = Enumerable.Range(0, 10000).Select(s => s.ToString());
bigList.ToObservable().Buffer(ChunkSize).Subscribe(
chunk => bag.Add(
Task.Factory.StartNew(
() =>
{
foreach (string s in chunk)
{
Console.WriteLine(s);
}
Console.WriteLine(String.Empty.PadRight(80, '-'));
},
cancellationToken,
TaskCreationOptions.None,
workStealingTaskScheduler)));
Task.WaitAll(bag.ToArray());
}
#endregion
}
}
于 2013-11-12T18:18:50.793 回答