1

This is probably a dumb question, but I'm wondering how I would fill a list with the following data for a CSV file.

Here's the code so far,

class Info
{
    [CsvColumn(Name = "Lease Name", FieldIndex = 1)]
    public string leaseName2 { get; set; }

    [CsvColumn(Name = "Field Name", FieldIndex = 2)]
    public string fieldName2 { get; set; }

    [CsvColumn(Name = "Reservoir", FieldIndex = 3)]
    public string reservoir2 { get; set; }

    [CsvColumn(Name = "Operator", FieldIndex = 4)]
    public string operator2 { get; set; }

    [CsvColumn(Name = "County", FieldIndex = 5)]
    public string county2 { get; set; }

    [CsvColumn(Name = "State", FieldIndex = 6)]
    public string state2 { get; set; }

    [CsvColumn(Name = "Majo", FieldIndex = 7)]
    public string majo2 { get; set; }

    [CsvColumn(Name = "Resv Cat", FieldIndex = 8)]
    public string resvCat2 { get; set; }

    [CsvColumn(Name = "Discount Rate", FieldIndex = 9)]
    public double disRate2 { get; set; }

There are more columns I just did not want to list them all because that would be redundant. If anyone could help that would be greatly appreciated.

neontapir
  • 4,698
  • 3
  • 37
  • 52
Stock
  • 77
  • 3
  • 15
  • 2
    It's not clear what you're asking. Are you wanting to take the contents of a CSV file and put it into a `List`? – neontapir May 30 '13 at 21:06
  • 1
    Or, are you wanting the reverse: http://stackoverflow.com/questions/2306667/how-can-i-convert-a-list-of-objects-to-csv – neontapir May 30 '13 at 21:07
  • @neontapir Sorry, I have two files that are being merged and I want the contents of that file to go into a list with these so I could create a CSV file – Stock May 30 '13 at 21:16

2 Answers2

2

@GertArnold is right, you can use Union(). I used Concat() in my example. Union returns distinct records, Concat doesn't.

using System;
using System.Collections.Generic;
using System.Linq;
using LINQtoCSV;

namespace LinqCsvSandbox
{
class SampleData
{
    [CsvColumn(Name = "ID", FieldIndex = 1)]
    public string ID { get; set; }

    [CsvColumn(Name = "PersonName", FieldIndex = 2)]
    public string Name { get; set; }

    public override string ToString()
    {
        return string.Format("{0}: {1}", ID, Name);
    }
}

class Program
{
    static void Main(string[] args)
    {           
        var inputFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',', 
            FirstLineHasColumnNames = false,
            FileCultureName = "en-us",
            EnforceCsvColumnAttribute = true,
        };

        CsvContext cc = new CsvContext();

        IEnumerable<SampleData> data1 = cc.Read<SampleData>("File1.csv", inputFileDescription);
        IEnumerable<SampleData> data2 = cc.Read<SampleData>("File2.csv", inputFileDescription);

        IEnumerable<SampleData> all = data1.Concat(data2);

        // Uncomment to see the items printed
        //foreach(var item in all)
        //  Console.WriteLine(item);

        cc.Write(all, "All.csv");           
    }
}
}

In my example, File1.csv contains

1,Fred
2,Wilma

File2.csv contains

3,Tango
4,Cash

And the resulting All.csv contains

ID,PersonName
1,Fred
2,Wilma
3,Tango
4,Cash

For those unfamiliar, Linq to CSV is available as a package from NuGet.

neontapir
  • 4,698
  • 3
  • 37
  • 52
  • Thank you, this is very helpful! Instead of the data1 and data2 being .csv files could they also be .txt files and work the same way? – Stock May 30 '13 at 21:41
0

Here are a couple of ways, the first is just using an object initializer, the second transfers a small list to the your CSV list:

List<Info> infoList = new List<Info>
        {
            new Info()
            {
                leaseName2 = "x"
            }

        };

    List<string> stringList = new List<string> {"xy", "zz"};

    infoList.AddRange(stringList.Select(stringVal => new Info()
        {
            leaseName2 = stringVal
        }));
bigtech
  • 464
  • 3
  • 6