C# 如何将文本字符串数组的数量限制为15

C# 如何将文本字符串数组的数量限制为15,c#,C#,如何将一次从文档中提取的行限制为15行。现在它一次显示所有行。谢谢 class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines( @"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt"); foreach (string line in lines) {

如何将一次从文档中提取的行限制为15行。现在它一次显示所有行。谢谢

class Program {
  static void Main(string[] args) {
    string[] lines = System.IO.File.ReadAllLines(
        @"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt");

    foreach (string line in lines) {
        Console.WriteLine("{0}", line);
    }

    Console.ReadKey();
  }
}
如果您只需要最上面的
15行
,请尝试专门为此设计的(Linq):

var lines = System.IO.File
  .ReadLines(@"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt")
  .Take(15);
如果您想要批量处理,即获取
0。。14行,然后是15行。。29
行等

// Split input into batches with at most "size" items each
private static IEnumerable<T[]> Batch<T>(IEnumerable<T> lines, int size) {
  List<T> batch = new List<T>(size);

  foreach (var item in lines) {
    if (batch.Count >= size) {
      yield return batch.ToArray();

      batch.Clear();
    }

    batch.Add(item);
  }

  if (batch.Count > 0)   // tail, possibly incomplete batch
    yield return batch.ToArray();
}

如果我理解正确,您可以使用
System.IO.File.ReadLines
来执行此操作,它允许您将文件作为
IEnumerable
流式传输。我创建了一个自定义批处理函数,它一次可以读取15行

static void Main(string[] args)
{
    var lines = System.IO.File.ReadLines(@"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt");
    foreach (var batch in Batch(lines, 15))
    {
        foreach (var line in batch)
        {
            Console.WriteLine(line);
        }
        Console.ReadKey();
    }
    Console.ReadKey();
}
这将返回文件中每个
batchSize
(例如15)行的列表

private IEnumerable<List<T>> Batch<T>(IEnumerable<T> source, int batchSize)
{
    if (batchSize < 1)
    {
        throw new ArgumentException("Batch size must be at least 1.", nameof(batchSize));
    }
    var batch = new List<T>(batchSize);
    foreach (var item in source)
    {
        batch.Add(item);
        if (batch.Count == batchSize)
        {
            yield return batch;
            batch = new List<T>(batchSize);
        }
    }
    if (batch.Any())
    {
        yield return batch;
    }
}
private IEnumerable批处理(IEnumerable源代码,int batchSize)
{
如果(批量大小<1)
{
抛出新ArgumentException(“批大小必须至少为1.”,nameof(batchSize));
}
var batch=新列表(batchSize);
foreach(源中的var项)
{
批次。添加(项目);
if(batch.Count==batchSize)
{
退货批量;
批次=新列表(批次大小);
}
}
if(batch.Any())
{
退货批量;
}
}

您是在问如何一次只读取文件中的15行(即替换您的file.ReadAllLines调用)还是如何以15行为一块处理所有行?@tolanj这就是问题所在。德米特里把它解释为前者,我把它解释为后者。我读到“一次”意思是“在循环中”(即“我正在处理一个大文档,不想一次将其全部存储在内存中”)。如果您连续调用两次,则可能会出现重复的
System.IO.File.ReadLines(MyFile)。取(15)
您会先得到15行第一行,然后得到16到30行吗?@Cid:No;您将两次获得顶部
15
行;如果你想跳过15行,然后只跳过15行-
.skip(15)。take(15)
谢谢你的回答,如果你想一块一块地获取行,这将变得很容易实现,尽管使用
.skip(BlockNumber++*BlockSize)。take(BlockSize)
@Cid:对于文件,
.skip(n)。take(m)
效率低下:我们不断地重新读取文件;我编辑了答案:我们应该只读取一次文件,并在读取时形成批处理。
private IEnumerable<List<T>> Batch<T>(IEnumerable<T> source, int batchSize)
{
    if (batchSize < 1)
    {
        throw new ArgumentException("Batch size must be at least 1.", nameof(batchSize));
    }
    var batch = new List<T>(batchSize);
    foreach (var item in source)
    {
        batch.Add(item);
        if (batch.Count == batchSize)
        {
            yield return batch;
            batch = new List<T>(batchSize);
        }
    }
    if (batch.Any())
    {
        yield return batch;
    }
}