【问题标题】:Combine contents of two files using LINQ to CSV使用 LINQ to CSV 合并两个文件的内容
【发布时间】:2013-05-26 14:23:02
【问题描述】:

这可能是一个愚蠢的问题,但我想知道如何用 CSV 文件的以下数据填充列表。

这是到目前为止的代码,

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; }

还有更多的列,我只是不想全部列出,因为那将是多余的。如果有人可以提供帮助,将不胜感激。

【问题讨论】:

  • 不清楚你在问什么。您是否要获取 CSV 文件的内容并将其放入 List<Info>
  • 或者,你想要相反的:stackoverflow.com/questions/2306667/…
  • @neontapir 抱歉,我有两个文件正在合并,我希望该文件的内容与这些文件一起进入一个列表,以便我可以创建一个 CSV 文件

标签: c# linq list csv


【解决方案1】:

@GertArnold 是对的,你可以使用Union()。我在示例中使用了Concat()。 Union 返回不同的记录,Concat 不返回。

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");           
    }
}
}

在我的示例中,File1.csv 包含

1,Fred
2,Wilma

File2.csv 包含

3,Tango
4,Cash

生成的 All.csv 包含

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

对于那些不熟悉的人,Linq to CSV 可作为 NuGet 的包提供。

【讨论】:

  • 谢谢,这很有帮助!除了 data1 和 data2 是 .csv 文件之外,它们还可以是 .txt 文件并以相同的方式工作吗?
【解决方案2】:

这里有几种方法,第一种只是使用对象初始化器,第二种将一个小列表传输到您的 CSV 列表:

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
        }));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2013-04-22
    • 2020-11-22
    • 1970-01-01
    • 2011-06-23
    • 2020-08-22
    • 1970-01-01
    相关资源
    最近更新 更多