Use LINQ to read delimiter text files

Source: Internet
Author: User

Sometimes we process text files with delimiters. For example, data separated by commas (,), such:

Then they are stored in text files with the following columns:

  • First Name
  • Last Name
  • Job Title
  • City
  • Country

     

    Before reading this file, create an object class:

       1:      /// <summary>
       2:      /// Customer entity
       3:      /// </summary>
       4:      /// Author Petter Liu http://wintersun.cnblogs.com
       5:      public class Customer
       6:      {
       7:          public string Firstname { get; set; }
       8:          public string Lastname { get; set; }
       9:          public string JobTitle { get; set; }
      10:          public string City { get; set; }
      11:          public string Country { get; set; }
      12:      }


    Next, we use LINQ to read the entire file:

       1:              var query = from line in File.ReadAllLines(filePath)
       2:                          let customerRecord = line.Split(,)
       3:                          select new Customer()
       4:                          {
       5:                              Firstname = customerRecord[0],
       6:                              Lastname = customerRecord[1],
       7:                              JobTitle = customerRecord[2],
       8:                              City = customerRecord[3],
       9:                              Country = customerRecord[4]
      10:                          };
      11:              foreach (var item in query)
      12:              {
      13:                  Console.WriteLine("{0}, {1}, {2}, {3}, {4}"
      14:                    , item.Firstname, item.Lastname, item.JobTitle, item.City, item.Country);
      15:              }

     

    To read a record that can contain conditions, we can filter out that Country is UK:

       1:              var query = from c in
       2:                              (from line in File.ReadAllLines(filePath)
       3:                               let customerRecord = line.Split(,)
       4:                               select new Customer()
       5:                               {
       6:                                   Firstname = customerRecord[0],
       7:                                   Lastname = customerRecord[1],
       8:                                   JobTitle = customerRecord[2],
       9:                                   City = customerRecord[3],
      10:                                   Country = customerRecord[4]
      11:                               })
      12:                          where c.Country == "UK"
      13:                          select c;

     

    Another example:

       1:              var query = from c in
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.