0

I have a CSV that contains a list of parts, and different columns showing whether we have pictures of different production steps for each part. Like, first column is part name, second is pattern info, etc.

I can put in an array of the parts we might be working on in a given day.

I want to parse the CSV into an array or whatnot, compare the first column of each line to see if it matches anything on the list of parts for the day, and then print off the entire row of data from the CSV if there's a match.

I have done something like this in Matlab, but because of not having Matlab license for work I am trying to do this in C#, which I am quite new at, so I have the basic idea of what I need to do, but understanding the correct way to do it in C# is something I haven't really grasped yet.

So I've been trying to follow along with one example I found for parsing CSV but I'm just kinda stuck understanding how to modify it for what I want to do.

class Picture{
    public string Number;
    public string Pattern;
    public string Set;
    public string Mold;
    public string Wash;
    public string Core;
    public string Close;

    public static Picture FromLine (string line)
    {   

        var data = line.Split(',');
        return new Picture()
        {
             Number = data[0],
             Pattern = data[1],
             Set = data[2],
             Mold = data[3],
             Wash = data[4],
             Core = data[5],
             Close =data[6],
         };
    }
}

class Program
{
    static void Main(string[] args)
    {
        string[] partList = {"cd-6566","CC-0526"};
        args = new[]{"ProcessPics.csv"};
        var part = ReadPart(args[0]);
        Console.WriteLine("Hello World!");
        Console.WriteLine("Help");


    }

    static IList<Picture> ReadPart(string path)
    {
        var list = new List<Picture>();
        foreach (var line in File.ReadLines(path).Skip(1))
        {
            list.Add(Picture.FromLine(line));
            Console.WriteLine("check");
        }
        return list;
    }


}
JohnDDW
  • 3
  • 1
  • Possible duplicate of [Parsing CSV files in C#, with header](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) – mjwills Jan 31 '19 at 12:33
  • What happens when you run this code? – Chetan Jan 31 '19 at 12:39
  • Make an array of todays parts : string[] todayParts = {"A","B","C"}; Then from your list of parts : Picture[] filteredParts = part.Where(x => todaysParts.Contain(x.Number)).ToArray() – jdweng Jan 31 '19 at 12:43
  • @ChetanRanpariya I get a console print of 'check' for each item on the CSV, which i put in there to make sure it was going through and parsing each line, then of course a single "hello world" and "help" in the console which were also just kinda there to make sure it was working. – JohnDDW Jan 31 '19 at 14:14

1 Answers1

0

In your row loop you want to check if the first column (= Picture.Number) is contained in partList:

    foreach (var line in File.ReadLines(path).Skip(1))
    {
        var picture = Picture.FromLine(line);
        if (!partList.Contains(picture.Number)) continue;
        list.Add(picture);
        Console.WriteLine("check");
    }
Markus Dresch
  • 5,290
  • 3
  • 20
  • 40