repl.it
@jfbueno/

pt.so/402594

C#

https://pt.stackoverflow.com/a/402608/18246

fork
loading
Files
  • main.cs
  • dados.csv
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;

class MainClass 
{
    public static void Main (string[] args) 
    {
        ReadRecord();
    }

    public static void ReadRecord()
    {
        var dadosEncontrados = ReadRecord ("1190", "dados.csv", 9);
        foreach(var itemEncontrado in dadosEncontrados) 
        {
            Console.WriteLine(string.Join(" ", itemEncontrado));
        }
        
        Console.ReadLine();
    }

    public static IEnumerable<string[]> ReadRecord(string searchTerm, string filepath, int positionOfSearchTerm)
    {
        positionOfSearchTerm--;

        string[] lines = System.IO.File.ReadAllLines(filepath);

        var retorno = new List<string[]>();
        for (int i = 0; i < lines.Length; i++)
        {
            string[] fields = lines[i].Split(',');
            if (recordMatches(searchTerm, fields, positionOfSearchTerm))
            {
                Console.WriteLine("Record found");
                retorno.Add(fields);
            }
        }

        return retorno;    
    }

    public static bool recordMatches(string searchTerm, string[] record, int positionOfSearchTerm)
    {
        return record[positionOfSearchTerm].Equals(searchTerm);
    }
}