다음을 통해 공유


방법: LINQ를 사용하여 파일 및 디렉터리 쿼리

대부분의 파일 시스템 작업은 기본적으로 쿼리이므로 LINQ 접근 방식에 적합합니다. 이러한 쿼리는 파괴적이지 않습니다. 원본 파일 또는 폴더의 내용은 변경되지 않습니다. 쿼리는 부작용을 일으키면 안 됩니다. 일반적으로 원본 데이터를 수정하는 모든 코드(만들기/업데이트/삭제 작업을 수행하는 쿼리 포함)는 데이터를 쿼리하는 코드와 별도로 유지되어야 합니다.

파일 시스템의 내용을 정확하게 나타내고 예외를 정상적으로 처리하는 데이터 원본을 만드는 데는 몇 가지 복잡성이 있습니다. 이 섹션의 예제에서는 지정된 루트 폴더 아래의 모든 파일과 모든 하위 폴더를 나타내는 개체의 FileInfo 스냅샷 컬렉션을 만듭니다. 쿼리 실행을 시작하고 종료하는 시간 사이에 각 FileInfo 항목의 실제 상태가 변경될 수 있습니다. 예를 들어 데이터 원본으로 사용할 개체 목록을 FileInfo 만들 수 있습니다. 쿼리에서 Length 속성에 접근하려고 하면, FileInfo 개체는 파일 시스템에 접근하여 Length의 값을 업데이트하려고 합니다. 파일이 더 이상 존재하지 않는 경우, 파일 시스템을 직접 쿼리하지 않더라도 쿼리에서 FileNotFoundException가 나타납니다.

지정된 특성 또는 이름을 사용하여 파일을 쿼리하는 방법

이 예제에서는 지정된 디렉터리 트리에서 지정된 파일 이름 확장명(예: ".txt")이 있는 모든 파일을 찾는 방법을 보여 줍니다. 또한 생성 시간에 따라 트리에서 가장 최신 또는 가장 오래된 파일을 반환하는 방법도 보여줍니다. Windows, Mac 또는 Linux 시스템에서 이 코드를 실행하는지 여부에 관계없이 많은 샘플의 첫 번째 줄을 수정해야 할 수 있습니다.

string startFolder = """C:\Program Files\dotnet\sdk""";
// Or
// string startFolder = "/usr/local/share/dotnet/sdk";

DirectoryInfo dir = new DirectoryInfo(startFolder);
var fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);

var fileQuery = from file in fileList
                where file.Extension == ".txt"
                orderby file.Name
                select file;

// Uncomment this block to see the full query
// foreach (FileInfo fi in fileQuery)
// {
//    Console.WriteLine(fi.FullName);
// }

var newestFile = (from file in fileQuery
                  orderby file.CreationTime
                  select new { file.FullName, file.CreationTime })
                  .Last();

Console.WriteLine($"\r\nThe newest .txt file is {newestFile.FullName}. Creation time: {newestFile.CreationTime}");

확장 프로그램별로 파일을 그룹화하는 방법

이 예제에서는 LINQ를 사용하여 파일 또는 폴더 목록에서 고급 그룹화 및 정렬 작업을 수행하는 방법을 보여 줍니다. 또한 콘솔 창에서 SkipTake 메서드를 사용하여 출력을 페이지하는 방법을 보여 줍니다.

다음 쿼리는 지정된 디렉터리 트리의 내용을 파일 이름 확장명별로 그룹화하는 방법을 보여 줍니다.

string startFolder = """C:\Program Files\dotnet\sdk""";
// Or
// string startFolder = "/usr/local/share/dotnet/sdk";

int trimLength = startFolder.Length;

DirectoryInfo dir = new DirectoryInfo(startFolder);

var fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);

var queryGroupByExt = from file in fileList
                      group file by file.Extension.ToLower() into fileGroup
                      orderby fileGroup.Count(), fileGroup.Key
                      select fileGroup;

// Iterate through the outer collection of groups.
foreach (var filegroup in queryGroupByExt.Take(5))
{
    Console.WriteLine($"Extension: {filegroup.Key}");
    var resultPage = filegroup.Take(20);

    //Execute the resultPage query
    foreach (var f in resultPage)
    {
        Console.WriteLine($"\t{f.FullName.Substring(trimLength)}");
    }
    Console.WriteLine();
}

프로그램의 출력은 로컬 파일 시스템의 세부 사항과 startFolder 설정에 따라 길어질 수 있습니다. 모든 결과를 볼 수 있도록 하기 위해 이 예제에서는 결과를 페이지를 넘기는 방법을 보여 줍니다. 각 그룹은 별도로 열거되므로 중첩 foreach 루프가 필요합니다.

폴더 집합의 총 바이트 수를 쿼리하는 방법

이 예제에서는 지정된 폴더의 모든 파일과 모든 하위 폴더에서 사용되는 총 바이트 수를 검색하는 방법을 보여 있습니다. 이 메서드는 Sum 절에서 select 선택한 모든 항목의 값을 추가합니다. 대신 또는 Min 메서드Max를 호출하여 지정된 디렉터리 트리에서 가장 큰 파일 또는 가장 작은 파일을 검색하도록 이 쿼리를 Sum 수정할 수 있습니다.

string startFolder = """C:\Program Files\dotnet\sdk""";
// Or
// string startFolder = "/usr/local/share/dotnet/sdk";

var fileList = Directory.GetFiles(startFolder, "*.*", SearchOption.AllDirectories);

var fileQuery = from file in fileList
                let fileLen = new FileInfo(file).Length
                where fileLen > 0
                select fileLen;

// Cache the results to avoid multiple trips to the file system.
long[] fileLengths = fileQuery.ToArray();

// Return the size of the largest file
long largestFile = fileLengths.Max();

// Return the total number of bytes in all the files under the specified folder.
long totalBytes = fileLengths.Sum();

Console.WriteLine($"There are {totalBytes} bytes in {fileList.Count()} files under {startFolder}");
Console.WriteLine($"The largest file is {largestFile} bytes.");

이 예제에서는 다음을 수행하도록 이전 예제를 확장합니다.

  • 가장 큰 파일의 크기(바이트)를 검색하는 방법입니다.
  • 가장 작은 파일의 크기(바이트)를 검색하는 방법입니다.
  • 지정된 루트 폴더 아래 FileInfo 의 하나 이상의 폴더에서 가장 크거나 작은 개체 파일을 검색하는 방법입니다.
  • 가장 큰 10개 파일과 같은 시퀀스를 검색하는 방법입니다.
  • 지정된 크기보다 작은 파일을 무시하고 파일 크기를 바이트 단위로 기준으로 파일을 그룹으로 정렬하는 방법입니다.

다음 예제에는 파일 크기(바이트)에 따라 파일을 쿼리하고 그룹화하는 방법을 보여 주는 5개의 개별 쿼리가 포함되어 있습니다. 이러한 예제를 수정하여 개체의 다른 속성에 대한 쿼리를 FileInfo 기반으로 할 수 있습니다.

// Return the FileInfo object for the largest file
// by sorting and selecting from beginning of list
FileInfo longestFile = (from file in fileList
                        let fileInfo = new FileInfo(file)
                        where fileInfo.Length > 0
                        orderby fileInfo.Length descending
                        select fileInfo
                        ).First();

Console.WriteLine($"The largest file under {startFolder} is {longestFile.FullName} with a length of {longestFile.Length} bytes");

//Return the FileInfo of the smallest file
FileInfo smallestFile = (from file in fileList
                         let fileInfo = new FileInfo(file)
                         where fileInfo.Length > 0
                         orderby fileInfo.Length ascending
                         select fileInfo
                        ).First();

Console.WriteLine($"The smallest file under {startFolder} is {smallestFile.FullName} with a length of {smallestFile.Length} bytes");

//Return the FileInfos for the 10 largest files
var queryTenLargest = (from file in fileList
                       let fileInfo = new FileInfo(file)
                       let len = fileInfo.Length
                       orderby len descending
                       select fileInfo
                      ).Take(10);

Console.WriteLine($"The 10 largest files under {startFolder} are:");

foreach (var v in queryTenLargest)
{
    Console.WriteLine($"{v.FullName}: {v.Length} bytes");
}

// Group the files according to their size, leaving out
// files that are less than 200000 bytes.
var querySizeGroups = from file in fileList
                      let fileInfo = new FileInfo(file)
                      let len = fileInfo.Length
                      where len > 0
                      group fileInfo by (len / 100000) into fileGroup
                      where fileGroup.Key >= 2
                      orderby fileGroup.Key descending
                      select fileGroup;

foreach (var filegroup in querySizeGroups)
{
    Console.WriteLine($"{filegroup.Key}00000");
    foreach (var item in filegroup)
    {
        Console.WriteLine($"\t{item.Name}: {item.Length}");
    }
}

하나 이상의 전체 FileInfo 개체를 반환하려면 먼저 쿼리에서 데이터 원본의 각 개체를 검사한 다음 Length 속성 값을 기준으로 정렬해야 합니다. 그런 다음 가장 긴 길이를 가진 단일 항목 또는 시퀀스를 반환할 수 있습니다. 목록의 첫 번째 요소를 반환하는 데 사용합니다 First . 처음 n 개수의 요소를 반환하는 데 사용합니다 Take . 목록의 시작 부분에 가장 작은 요소를 배치하려면 내림차순 정렬 순서를 지정합니다.

디렉터리 트리에서 중복 파일을 쿼리하는 방법

이름이 같은 파일을 둘 이상의 폴더에 배치할 수 있는 경우도 있습니다. 이 예제에서는 지정된 루트 폴더에서 이러한 중복 파일 이름을 쿼리하는 방법을 보여 있습니다. 두 번째 예제에서는 크기 및 LastWrite 시간도 일치하는 파일을 쿼리하는 방법을 보여줍니다.

string startFolder = """C:\Program Files\dotnet\sdk""";
// Or
// string startFolder = "/usr/local/share/dotnet/sdk";

DirectoryInfo dir = new DirectoryInfo(startFolder);

IEnumerable<FileInfo> fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);

// used in WriteLine to keep the lines shorter
int charsToSkip = startFolder.Length;

// var can be used for convenience with groups.
var queryDupNames = from file in fileList
                    group file.FullName.Substring(charsToSkip) by file.Name into fileGroup
                    where fileGroup.Count() > 1
                    select fileGroup;

foreach (var queryDup in queryDupNames.Take(20))
{
    Console.WriteLine($"Filename = {(queryDup.Key.ToString() == string.Empty ? "[none]" : queryDup.Key.ToString())}");

    foreach (var fileName in queryDup.Take(10))
    {
        Console.WriteLine($"\t{fileName}");
    }   
}

첫 번째 쿼리는 키를 사용하여 일치 항목을 확인합니다. 이름이 같지만 내용이 다를 수 있는 파일을 찾습니다. 두 번째 쿼리는 복합 키를 사용하여 개체의 세 가지 속성과 일치합니다 FileInfo . 이 쿼리는 이름이 같고 콘텐츠가 비슷하거나 동일한 파일을 찾을 가능성이 훨씬 높습니다.

    string startFolder = """C:\Program Files\dotnet\sdk""";
    // Or
    // string startFolder = "/usr/local/share/dotnet/sdk";

    // Make the lines shorter for the console display
    int charsToSkip = startFolder.Length;

    // Take a snapshot of the file system.
    DirectoryInfo dir = new DirectoryInfo(startFolder);
    IEnumerable<FileInfo> fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);

    // Note the use of a compound key. Files that match
    // all three properties belong to the same group.
    // A named type is used to enable the query to be
    // passed to another method. Anonymous types can also be used
    // for composite keys but cannot be passed across method boundaries
    //
    var queryDupFiles = from file in fileList
                        group file.FullName.Substring(charsToSkip) by
                        (Name: file.Name, LastWriteTime: file.LastWriteTime, Length: file.Length )
                        into fileGroup
                        where fileGroup.Count() > 1
                        select fileGroup;

    foreach (var queryDup in queryDupFiles.Take(20))
    {
        Console.WriteLine($"Filename = {(queryDup.Key.ToString() == string.Empty ? "[none]" : queryDup.Key.ToString())}");

        foreach (var fileName in queryDup)
        {
            Console.WriteLine($"\t{fileName}");
        }
    }
}

폴더에서 텍스트 파일의 내용을 쿼리하는 방법

이 예제에서는 지정된 디렉터리 트리의 모든 파일을 쿼리하고, 각 파일을 열고, 해당 내용을 검사하는 방법을 보여 줍니다. 이러한 유형의 기술을 사용하여 디렉터리 트리 내용의 인덱스 또는 역방향 인덱스를 만들 수 있습니다. 이 예제에서는 간단한 문자열 검색이 수행됩니다. 그러나 정규식을 사용하여 더 복잡한 형식의 패턴 일치를 수행할 수 있습니다.

string startFolder = """C:\Program Files\dotnet\sdk""";
// Or
// string startFolder = "/usr/local/share/dotnet/sdk";

DirectoryInfo dir = new DirectoryInfo(startFolder);

var fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);

string searchTerm = "change";

var queryMatchingFiles = from file in fileList
                         where file.Extension == ".txt"
                         let fileText = File.ReadAllText(file.FullName)
                         where fileText.Contains(searchTerm)
                         select file.FullName;

// Execute the query.
Console.WriteLine($"""The term "{searchTerm}" was found in:""");
foreach (string filename in queryMatchingFiles)
{
    Console.WriteLine(filename);
}

두 폴더의 내용을 비교하는 방법

이 예제에서는 두 파일 목록을 비교하는 세 가지 방법을 보여 줍니다.

  • 두 파일 목록이 동일한지 여부를 지정하는 부울 값을 쿼리합니다.
  • 교차를 쿼리하여 두 폴더에 있는 파일을 검색합니다.
  • 집합 차이를 쿼리하여 한 폴더에 있지만 다른 폴더에는 없는 파일을 검색합니다.

여기에 표시된 기술은 모든 형식의 개체 시퀀스를 비교하도록 조정할 수 있습니다.

여기에 표시된 클래스는 FileComparer 표준 쿼리 연산자와 함께 사용자 지정 비교자 클래스를 사용하는 방법을 보여 줍니다. 이 클래스는 실제 시나리오에서 사용하기 위한 것이 아닙니다. 각 파일의 이름과 길이(바이트)를 사용하여 각 폴더의 내용이 동일한지 여부를 확인합니다. 실제 시나리오에서는 보다 엄격한 같음 검사를 수행하도록 이 비교자를 수정해야 합니다.

// This implementation defines a very simple comparison
// between two FileInfo objects. It only compares the name
// of the files being compared and their length in bytes.
class FileCompare : IEqualityComparer<FileInfo>
{
    public bool Equals(FileInfo? f1, FileInfo? f2)
    {
        return (f1?.Name == f2?.Name &&
                f1?.Length == f2?.Length);
    }

    // Return a hash that reflects the comparison criteria. According to the
    // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
    // also be equal. Because equality as defined here is a simple value equality, not
    // reference identity, it is possible that two or more objects will produce the same
    // hash code.
    public int GetHashCode(FileInfo fi)
    {
        string s = $"{fi.Name}{fi.Length}";
        return s.GetHashCode();
    }
}

public static void CompareDirectories()
{
    string pathA = """C:\Program Files\dotnet\sdk\8.0.104""";
    string pathB = """C:\Program Files\dotnet\sdk\8.0.204""";

    DirectoryInfo dir1 = new DirectoryInfo(pathA);
    DirectoryInfo dir2 = new DirectoryInfo(pathB);

    IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories);
    IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);

    //A custom file comparer defined below
    FileCompare myFileCompare = new FileCompare();

    // This query determines whether the two folders contain
    // identical file lists, based on the custom file comparer
    // that is defined in the FileCompare class.
    // The query executes immediately because it returns a bool.
    bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

    if (areIdentical == true)
    {
        Console.WriteLine("the two folders are the same");
    }
    else
    {
        Console.WriteLine("The two folders are not the same");
    }

    // Find the common files. It produces a sequence and doesn't
    // execute until the foreach statement.
    var queryCommonFiles = list1.Intersect(list2, myFileCompare);

    if (queryCommonFiles.Any())
    {
        Console.WriteLine($"The following files are in both folders (total number = {queryCommonFiles.Count()}):");
        foreach (var v in queryCommonFiles.Take(10))
        {
            Console.WriteLine(v.Name); //shows which items end up in result list
        }
    }
    else
    {
        Console.WriteLine("There are no common files in the two folders.");
    }

    // Find the set difference between the two folders.
    var queryList1Only = (from file in list1
                          select file)
                          .Except(list2, myFileCompare);

    Console.WriteLine();
    Console.WriteLine($"The following files are in list1 but not list2 (total number = {queryList1Only.Count()}):");
    foreach (var v in queryList1Only.Take(10))
    {
        Console.WriteLine(v.FullName);
    }

    var queryList2Only = (from file in list2
                          select file)
                          .Except(list1, myFileCompare);

    Console.WriteLine();
    Console.WriteLine($"The following files are in list2 but not list1 (total number = {queryList2Only.Count()}:");
    foreach (var v in queryList2Only.Take(10))
    {
        Console.WriteLine(v.FullName);
    }
}

구분된 파일의 필드 순서를 다시 지정하는 방법

CSV(쉼표로 구분된 값) 파일은 스프레드시트 데이터 또는 행과 열이 나타내는 다른 테이블 형식 데이터를 저장하는 데 자주 사용되는 텍스트 파일입니다. 이 메서드를 Split 사용하여 필드를 구분하면 LINQ를 사용하여 CSV 파일을 쉽게 쿼리하고 조작할 수 있습니다. 실제로 동일한 기술을 사용하여 구조화된 텍스트 줄의 부분 순서를 변경할 수 있습니다. CSV 파일로 제한되지 않습니다.

다음 예제에서는 세 개의 열이 학생의 "가족 이름", "이름" 및 "ID"를 나타낸다고 가정합니다. 필드는 학생의 가족 이름을 기준으로 사전순으로 표시됩니다. 이 쿼리는 ID 열이 먼저 나타나는 새 시퀀스를 생성한 다음, 학생의 이름과 패밀리 이름을 결합하는 두 번째 열을 생성합니다. 줄의 순서는 ID 필드에 따라 다시 정렬됩니다. 결과는 새 파일에 저장되고 원래 데이터는 수정되지 않습니다. 다음 텍스트는 다음 예제에서 사용되는 spreadsheet1.csv 파일의 내용을 보여 줍니다.

Adams,Terry,120
Fakhouri,Fadi,116
Feng,Hanying,117
Garcia,Cesar,114
Garcia,Debra,115
Garcia,Hugo,118
Mortensen,Sven,113
O'Donnell,Claire,112
Omelchenko,Svetlana,111
Tucker,Lance,119
Tucker,Michael,122
Zabokritski,Eugene,121

다음 코드는 소스 파일을 읽고 CSV 파일의 각 열을 다시 정렬하여 열 순서를 다시 정렬합니다.

string[] lines = File.ReadAllLines("spreadsheet1.csv");

// Create the query. Put field 2 first, then
// reverse and combine fields 0 and 1 from the old field
IEnumerable<string> query = from line in lines
                            let fields = line.Split(',')
                            orderby fields[2]
                            select $"{fields[2]}, {fields[1]} {fields[0]}";

File.WriteAllLines("spreadsheet2.csv", query.ToArray());

/* Output to spreadsheet2.csv:
111, Svetlana Omelchenko
112, Claire O'Donnell
113, Sven Mortensen
114, Cesar Garcia
115, Debra Garcia
116, Fadi Fakhouri
117, Hanying Feng
118, Hugo Garcia
119, Lance Tucker
120, Terry Adams
121, Eugene Zabokritski
122, Michael Tucker
*/

그룹을 사용하여 파일을 여러 파일로 분할하는 방법

이 예제에서는 두 파일의 내용을 병합한 다음 새 방식으로 데이터를 구성하는 새 파일 집합을 만드는 한 가지 방법을 보여 줍니다. 쿼리는 두 파일의 내용을 사용합니다. 다음 텍스트는names1.txt첫 번째 파일의 내용을 보여 줍니다.

Bankov, Peter
Holm, Michael
Garcia, Hugo
Potra, Cristina
Noriega, Fabricio
Aw, Kam Foo
Beebe, Ann
Toyoshima, Tim
Guy, Wey Yuan
Garcia, Debra

names2.txt두 번째 파일에 는 다른 이름 집합이 포함되어 있으며, 그 중 일부는 첫 번째 집합과 공통적으로 있습니다.

Liu, Jinghao
Bankov, Peter
Holm, Michael
Garcia, Hugo
Beebe, Ann
Gilchrist, Beth
Myrcha, Jacek
Giakoumakis, Leo
McLin, Nkenge
El Yassir, Mehdi

다음 코드는 두 파일을 쿼리하고, 두 파일의 합집합을 취한 다음, 패밀리 이름의 첫 글자로 정의된 각 그룹에 대해 새 파일을 작성합니다.

string[] fileA = File.ReadAllLines("names1.txt");
string[] fileB = File.ReadAllLines("names2.txt");

// Concatenate and remove duplicate names
var mergeQuery = fileA.Union(fileB);

// Group the names by the first letter in the last name.
var groupQuery = from name in mergeQuery
                 let n = name.Split(',')[0]
                 group name by n[0] into g
                 orderby g.Key
                 select g;

foreach (var g in groupQuery)
{
    string fileName = $"testFile_{g.Key}.txt";

    Console.WriteLine(g.Key);

    using StreamWriter sw = new StreamWriter(fileName);
    foreach (var item in g)
    {
        sw.WriteLine(item);
        // Output to console for example purposes.
        Console.WriteLine($"   {item}");
    }
}
/* Output:
    A
       Aw, Kam Foo
    B
       Bankov, Peter
       Beebe, Ann
    E
       El Yassir, Mehdi
    G
       Garcia, Hugo
       Guy, Wey Yuan
       Garcia, Debra
       Gilchrist, Beth
       Giakoumakis, Leo
    H
       Holm, Michael
    L
       Liu, Jinghao
    M
       Myrcha, Jacek
       McLin, Nkenge
    N
       Noriega, Fabricio
    P
       Potra, Cristina
    T
       Toyoshima, Tim
 */

서로 다른 파일의 콘텐츠를 조인하는 방법

이 예제에서는 일치하는 키로 사용되는 공통 값을 공유하는 쉼표로 구분된 두 파일의 데이터를 조인하는 방법을 보여 있습니다. 이 기술은 두 스프레드시트 또는 스프레드시트의 데이터와 다른 형식의 파일을 새 파일로 결합해야 하는 경우에 유용할 수 있습니다. 모든 종류의 구조화된 텍스트로 작업하도록 예제를 수정할 수 있습니다.

다음 텍스트는 scores.csv내용을 보여 줍니다. 파일은 스프레드시트 데이터를 나타냅니다. 열 1은 학생의 ID이며, 2~5열은 시험 점수입니다.

111, 97, 92, 81, 60
112, 75, 84, 91, 39
113, 88, 94, 65, 91
114, 97, 89, 85, 82
115, 35, 72, 91, 70
116, 99, 86, 90, 94
117, 93, 92, 80, 87
118, 92, 90, 83, 78
119, 68, 79, 88, 92
120, 99, 82, 81, 79
121, 96, 85, 91, 60
122, 94, 92, 91, 91

다음 텍스트는 names.csv내용을 보여 줍니다. 이 파일은 학생의 가족 이름, 이름 및 학생 ID를 포함하는 스프레드시트를 나타냅니다.

Omelchenko,Svetlana,111
O'Donnell,Claire,112
Mortensen,Sven,113
Garcia,Cesar,114
Garcia,Debra,115
Fakhouri,Fadi,116
Feng,Hanying,117
Garcia,Hugo,118
Tucker,Lance,119
Adams,Terry,120
Zabokritski,Eugene,121
Tucker,Michael,122

관련 정보가 포함된 다른 파일의 콘텐츠를 조인합니다. 파일 names.csv 학생 이름과 ID 번호를 포함합니다. 파일 scores.csv ID와 4개의 테스트 점수 집합을 포함합니다. 다음 쿼리는 ID를 일치하는 키로 사용하여 점수를 학생 이름에 조인합니다. 코드는 다음 예제에 나와 있습니다.

string[] names = File.ReadAllLines(@"names.csv");
string[] scores = File.ReadAllLines(@"scores.csv");

var scoreQuery = from name in names
                  let nameFields = name.Split(',')
                  from id in scores
                  let scoreFields = id.Split(',')
                  where Convert.ToInt32(nameFields[2]) == Convert.ToInt32(scoreFields[0])
                  select $"{nameFields[0]},{scoreFields[1]},{scoreFields[2]},{scoreFields[3]},{scoreFields[4]}";

Console.WriteLine("\r\nMerge two spreadsheets:");
foreach (string item in scoreQuery)
{
    Console.WriteLine(item);
}
Console.WriteLine($"{scoreQuery.Count()} total names in list");
/* Output:
Merge two spreadsheets:
Omelchenko, 97, 92, 81, 60
O'Donnell, 75, 84, 91, 39
Mortensen, 88, 94, 65, 91
Garcia, 97, 89, 85, 82
Garcia, 35, 72, 91, 70
Fakhouri, 99, 86, 90, 94
Feng, 93, 92, 80, 87
Garcia, 92, 90, 83, 78
Tucker, 68, 79, 88, 92
Adams, 99, 82, 81, 79
Zabokritski, 96, 85, 91, 60
Tucker, 94, 92, 91, 91
12 total names in list
 */

CSV 텍스트 파일에서 열 값을 계산하는 방법

이 예제에서는 .csv 파일의 열에서 Sum, Average, Min 및 Max와 같은 집계 계산을 수행하는 방법을 보여 줍니다. 여기에 표시된 예제 원칙은 다른 형식의 구조화된 텍스트에 적용할 수 있습니다.

다음 텍스트는 scores.csv내용을 보여 줍니다. 첫 번째 열이 학생 ID를 나타내고 후속 열이 4개의 시험에서 점수를 나타낸다고 가정합니다.

111, 97, 92, 81, 60
112, 75, 84, 91, 39
113, 88, 94, 65, 91
114, 97, 89, 85, 82
115, 35, 72, 91, 70
116, 99, 86, 90, 94
117, 93, 92, 80, 87
118, 92, 90, 83, 78
119, 68, 79, 88, 92
120, 99, 82, 81, 79
121, 96, 85, 91, 60
122, 94, 92, 91, 91

다음 텍스트는 메서드를 사용하여 Split 각 텍스트 줄을 배열로 변환하는 방법을 보여줍니다. 각 배열 요소는 열을 나타냅니다. 마지막으로 각 열의 텍스트가 숫자 표현으로 변환됩니다.

public static class SumColumns
{
    public static void ProcessColumns(string filePath, string seperator)
    {
        // Divide each exam into a group
        var exams = from line in MatrixFrom(filePath, seperator)
                    from score in line

                    // Identify the column number
                    let colNumber = Array.FindIndex(line, t => ReferenceEquals(score, t))

                    // The first column is the student ID, not the exam score
                    // so it needs to be excluded
                    where colNumber > 0

                    // Convert the score from string to int
                    // Group by column number, i.e. one group per exam
                    group double.Parse(score) by colNumber into g
                    select new
                    {
                        Title = $"Exam#{g.Key}",
                        Min = g.Min(),
                        Max = g.Max(),
                        Avg = Math.Round(g.Average(), 2),
                        Total = g.Sum()
                    };

        foreach (var exam in exams)
        {
            Console.WriteLine($"{exam.Title}\t"
            + $"Average:{exam.Avg,6}\t"
            + $"High Score:{exam.Max,3}\t"
            + $"Low Score:{exam.Min,3}\t"
            + $"Total:{exam.Total,5}");
        }
    }

    // Transform the file content to an IEnumerable of string arrays
    // like a matrix
    private static IEnumerable<string[]> MatrixFrom(string filePath, string seperator)
    {
        using StreamReader reader = File.OpenText(filePath);

        for (string? line = reader.ReadLine(); line is not null; line = reader.ReadLine())
        {
            yield return line.Split(seperator, StringSplitOptions.TrimEntries);
        }
    }
}

// Output:
// Exam#1  Average: 86.08  High Score: 99  Low Score: 35   Total: 1033
// Exam#2  Average: 86.42  High Score: 94  Low Score: 72   Total: 1037
// Exam#3  Average: 84.75  High Score: 91  Low Score: 65   Total: 1017
// Exam#4  Average: 76.92  High Score: 94  Low Score: 39   Total:  923

파일이 탭으로 구분된 파일인 경우 메서드의 인수를 SumColumns.ProcessColumns 로 업데이트하면 됩니다 \t.