How to Load a Flat Text File Into a Table in C# and Use LINQ in Order to Query for Data Fields

Posted on 2011-10-26
Last Modified: 2013-12-16
I have a flat file that I am reading data from and I am currently reading the data into an ArrayList. I will need to update the application to be able to read in more than one data file, and I was wondering if someone could explain to me how I would read the data in from a *.dat file and use LINQ in order to get the specific fields from the data table that would be created from reading in the information from the flat file. (I do have business rules that split the flat data table into different fields, and I would like to be able to store these fields within a temporary table in memory so that I could query them and be able to pull back results based on the information a user enters. Could someone direct me to example code that completes this task or if they could help me with the syntax that I would need in order to accomplish this task?

Any help with this task would be greatly appreciated.
Question by:thenthorn1010
    1 Comment
    LVL 74

    Accepted Solution

    For Linq-to-DataTable, you can generally do something like:

    var query = from row in dt.Rows.OfType<DataRow>()
                where row.Field<string>(0) == "some value"
                select row;
    foreach (var item in query)

    Open in new window

    This is a very crude example, and you would have to change it to fit your scenario, but hopefully it demonstrates the concept. You can also select into a new existing or anonymous type if you don't want to return DataRows.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now