Click here to Skip to main content
15,885,689 members
Articles / Programming Languages / C#

LINQ to CSV library

Rate me:
Please Sign up or sign in to vote.
4.97/5 (217 votes)
10 Jan 2015Apache23 min read 996.8K   482  
Easy to use library to use CSV and tab delimited files with LINQ queries.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using LINQtoCSV;

namespace TestConsoleApplication
{
// Because the fields in this type are used only indirectly, the compiler
// will warn they are unused or unassigned. Disable those warnings.
#pragma warning disable 0169, 0414, 0649

    class ProductData
    {
        [CsvColumn(FieldIndex = 1)]
        public string name;

        // OutputFormat uses the same codes as the standard ToString method (search MSDN).
        [CsvColumn(FieldIndex = 2, OutputFormat = "d")]
        public DateTime startDate;

        [CsvColumn(FieldIndex = 3, OutputFormat = "dd MMM HH:mm:ss")]
        public DateTime launchTime;

        // Can use both fields and properties
        [CsvColumn(FieldIndex = 4, CanBeNull = false, OutputFormat = "#,000.000")]
        public double weight { get; set; }

        // Following field has no CsvColumn attribute.
        // So will be ignored when library is told to only use data with CsvColumn attribute.
        public int nbrAvailable;

        // Ok to have gaps in FieldIndex order
        [CsvColumn(FieldIndex = 10)]
        public string shopsAvailable;

        // Override field name, so in data files, this field is known as "code" instead of "hexProductCode"
        // This field contains hexadecimal numbers, without leading 0x.
        // This requires setting the NumberStyle property to NumberStyles.HexNumber.
        // Don't forget to import the namespace System.Globalization
        [CsvColumn(Name = "code", FieldIndex = 11, NumberStyle = NumberStyles.HexNumber)]
        public int hexProductCode;

        public string unusedField;

        // FieldIndex order higher then that of next field.
        // So this field will come AFTER next field in the actual data file
        [CsvColumn(FieldIndex = 16)]
        public bool onsale;

        // Override field name, so in data files, this field is known as "price" instead of "retailPrice"
        // OutputFormat uses the same codes as the standard ToString method (search MSDN). Format "C" is for currency.
        [CsvColumn(Name = "price", FieldIndex = 14, CanBeNull = false, OutputFormat = "C")]
        public decimal retailPrice { get; set; }
        
        [CsvColumn(FieldIndex = 30)]
        public string description;

#pragma warning restore 0169, 0414, 0649

        public override string ToString()
        {
            return 
                "name=" + (string.IsNullOrEmpty(name) ? "<null>" : name) + " | " +
                "startDate=" + startDate.ToString() + " | " +
                "launchTime=" + launchTime.ToString() + " | " +
                "weight=" + weight.ToString() + " | " +
                "nbrAvailable=" + nbrAvailable.ToString() + " | " +
                "shopsAvailable=" + ((shopsAvailable==null) ? "<null>" : shopsAvailable) + " | " +
                "hexProductCode=" + hexProductCode.ToString() + " | " +
                "unusedField=" + ((unusedField==null) ? "<null>" : unusedField) + " | " +
                "onsale=" + onsale.ToString() + " |  " +
                "retailPrice=" + retailPrice.ToString() + " | " +
                "description=" + ((description==null) ? "<null>" : description);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Architect
Australia Australia
Twitter: @MattPerdeck
LinkedIn: au.linkedin.com/in/mattperdeck
Current project: JSNLog JavaScript Logging Package

Matt has over 9 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks and the largest ticketing web site in Australia. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.

He is the author of the book ASP.NET Performance Secrets (www.amazon.com/ASP-NET-Site-Performance-Secrets-Perdeck/dp/1849690685) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser.

Matt currently lives in Sydney, Australia. He recently worked at Readify and the global professional services company PwC. He now works at SP Health, a global provider of weight loss web sites such at CSIRO's TotalWellBeingDiet.com and BiggestLoserClub.com.

Comments and Discussions