score:6
Accepted answer
Your code is already optimal to achieve the goal:
var list = new List<string>();
using (var reader = new StreamReader(fileName))
{
for (int i = 0; i < 10; i++)
{
list.Add(reader.ReadLine());
}
}
return list;
or
using (var reader = new StreamReader(fileName))
{
for (int i = 0; i < 10; i++)
{
yield return reader.ReadLine();
}
}
or
var r = File.ReadLines(fileName)
.Take(10) // limit to first 10
.ToArray(); // materialize, if needed
score:4
LINQ style:
using (var textReader = File.OpenText(fileName))
{
return Enumerable.Range(1, 10).Select(i => textReader.ReadLine());
}
score:10
You can use:
var lines = File.ReadLines(path).Take(10));
By using ReadLines
, rather than ReadAllLines
you will stream data from the file, rather than reading the entire thing into memory. If you are still on C# 3.5, not 4 (when ReadLines
was added) you can use the below implementation:
public static IEnumerable<string> ReadLines(string filename)
{
using (TextReader tr = new StreamReader(filename))
{
string nextLine = tr.ReadLine();
while (nextLine != null)
{
yield return nextLine;
nextLine = tr.ReadLine();
}
}
}
score:-1
May you interest this mix :)
using (var reader = new StreamReader(filename))
{
return (from p in Enumerable.Range(0, 10) select reader.ReadLine()).ToList();
}
Source: stackoverflow.com
Related Articles
- Read first 10 line from file using LINQ
- How to read File names recursively from subfolder using LINQ
- Read text data from file using LINQ
- Read from xml file to objectlist using Linq
- How to read data from xml file using linq to xml?
- Using LINQ to read key value pair from configuration file
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- How to read a CSV file using linq after the csv file is downloaded from Azure?
- Convert string[] to int[] in one line of code using LINQ
- How to get first object out from List<Object> using Linq
- Take the first five elements and the last five elements from an array by one query using LINQ
- read xml file using linq
- How to read XML file using System.IO.Stream with LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Selecting elements from XML file using LINQ
- C# .Net 3.5 Code to replace a file extension using LINQ
- reversing text from input file using linq
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- creating Linq to sqlite dbml from DbLinq source code
- Read text file word-by-word using LINQ
- read icollection data using LINQ in C# code
- Linq sub query when using a repository pattern with EF code first
- Lookup from xml file using linq
- How do I get the first value from this collection using Linq to Entities?
- Take unique records from text file using LINQ
- C# Read and filter CSV file using LINQ
- Read from CellSet using LINQ
- Linq - using not "IN" operator to select non duplicated lines from a text file
- Filtering folders from a list when using LINQ to sort to find the oldest file
- Can't add a new record with an integer value into database by using linq from code C#
- Is it possible to construct a stringbuilder using a lambda function instead of foreach?
- How to determine if a string contains any matches of a list of strings
- LINQ query returning null results
- Returning multiple streams from LINQ query
- LINQ Query IList<string> to JSon Format
- C# WebAPI Cross update inner join data with multiple tables
- counting integers in a list
- LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression
- Select result of where clause
- Linq: forward looking condition
- search database table record using two parameters in linq
- Simple LINQ query
- How to conditionally group multiple columns from a database with linq?
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- C# MVC Model Data Group on Razor View
- Selecting a property based on Name in Linq projection
- HTMLAgilityPack Exclude by classes
- How do you do a join in LinqToSQL?
- Check for null condition for left side table in left join in Linq
- Sorting issue with LINQ query and join using tables from different databases