score:12
You can use the code like that
var pairs = File.ReadAllLines("filename.txt")
.Select(line => line.Split(':'))
.ToDictionary(cells => cells[0].Trim(), cells => cells[1].Trim())
Or use the .NET 4.0 File.ReadLines() method to return an IEnumerable, which is useful for processing big text files.
score:5
Filehelpers is a really great open source solution to this:
http://filehelpers.sourceforge.net/
You just declare a class with attributes, and FileHelpers reads the flat file for you:
[FixedLengthRecord]
public class PriceRecord
{
[FieldFixedLength(6)]
public int ProductId;
[FieldFixedLength(8)]
[FieldConverter(typeof(MoneyConverter))]
public decimal PriceList;
[FieldFixedLength(8)]
[FieldConverter(typeof(MoneyConverter))]
public decimal PriceOnePay;
}
Once FileHelpers gives you back an array of rows, you can use Linq to Objects to query the data
We've had great success with it. I actually think Kaerber's solution is a nice simple solution, maybe stave of migrating to FileHelpers till you really need the extra power.
score:6
The concept of a text file data source is extremely broad (consider that XML is stored in text files). For that reason, I think it is unlikely that such a beast exists.
It should be simple enough to read the text file into a collection of Account objects and then use LINQ-to-Objects.
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- Reading Text Files with LINQ
- Linq To Text Files
- creating Linq to sqlite dbml from DbLinq source code
- Using LINQ to query flat text files with fixed-length records?
- source code for LINQ 101 samples
- C# Linq query search for a single keyword in multiple text files
- Trying to query many text files in the same folder with linq
- c# Linq or code to extract groups from a single list of source data
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How do I find the text within a div in the source of a web page using C#
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Full Text Search in Linq
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How/Can I use linq to xml to query huge xml files with reasonable memory consumption?
- Convert LINQ Expression to SQL Text without DB Context
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- How to build a LINQ query from text at runtime?
- Best open source LINQ provider
- Using text SQL with LINQ
- How can I sort a string of text followed by a number using LINQ
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
More Query from same tag
- Linq - Get the Index of the Last Non-Zero Number of Array
- LINQ query fitting into a model
- Converting a Parallel.ForEach loop into parallel tasks using lambda expression
- .NET 3.5 Linq Datasource and Joins
- Linq query to group items and query from the top item in each group
- Linq to SQL: Sum with multiple columns select
- Call function into view razor
- LINQ - How to get data from multiple tables
- When should I use .Count() and .Count in the context of an IEnumerable<T>
- Convert 2D array to string in C#, looking for most elegant way
- LINQ error - The method 'Sum' is not supported
- Why doesn't my function await the end of foreachAsync?
- Insert and update multi item into database in the same time
- How to Pivote grid view in C#
- How to manage multiple tables with the same structure
- Proper Syntax for LINQ query selecting only certain columns
- IDictionary extension not actually running while in LInq Query .Where()
- Converting SQL code with Row_Number() into LINQ C# code
- EF4.1 LINQ, selecting all results
- How to query datetime literals and null values in Linq predicate?
- Reading specific parts of a file
- Compare two list elements with LINQ
- Formatting field in a LINQ expression
- Unit testing collection contents meet certain criteria using lambdas/LINQ
- Search a part of a List<> based on the indext of a pattern match
- Is there a "not equal" in a linq join
- Trying to retrieve result using linq
- Getting data between values and greater than
- Build dynamic LINQ queries from a string - Use Reflection?
- Time date conversion in linq