使用 LINQ to CSV 合并两个文件的内容

问题描述 投票:0回答:3

这可能是一个愚蠢的问题,但我想知道如何使用 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; }

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

c# linq list csv
3个回答
2
投票

@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 的软件包使用。


0
投票

这里有几种方法,第一种是仅使用对象初始值设定项,第二种是将一个小列表传输到您的 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
        }));

0
投票

使用

SelectMany
,您可以“展平”可枚举的可枚举。

var cc= new CsvContext();

var files = new[] { "File1.csv", "File2.csv" };

var allData = files.SelectMany(csvFile => cc.Read<SampleData>(csvFile , csvFileDescription));

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.selectmany

© www.soinside.com 2019 - 2024. All rights reserved.