0

I have 2 files, both have a different amount of length in lines, however, there is a high probability that they share one value to each other that reside in different columns, example:

File1.csv = A,B,C,D,E (this file is 20 lines long)
File2.csv = A,B,D,C (no E and this file is 10 lines long)

How can I see if the value in the 3rd column of File1 is the same as the value in column 4 of File2, then also append file2's column 4 value to the end of each line in File1 (on match of eachother) and also not letting the loop end prematurely because File2 has less lines than File1?

First off, I think I'll need to look at setting up a linq statement similar to

        var file1lines = File.ReadLines(file1)
                    .Select(line => line.Split(','))
                    .Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
                    .ToList();

        var file2lines = File.ReadLines(file2)
                    .Select(line => line.Split(','))
                    .Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
                    .ToList();              

        var result = from a in file1lines.AsParallel()
                     from b in file2lines.AsParallel()
                     where a[2] == b[3]
                     select new { a, b, };
        using (StreamReader checkfiles = new StreamReader(file1)
        {
            foreach (var x in result)
            {
                // ...
            }
        }

Or would I even want to use a streamreader? or would I want 2 to open up both files so I can read them together (although I believe Linq accomplish this for me using .readlines) Am I just overcomplicating this whole scenario?

Thanks for any help or advice. I can't seem to wrap my head around what I want to do with the programming skills I currently have. It can't be that hard, so I'm missing something.

Michael Tucker
  • 309
  • 3
  • 14
  • 1
    It look like you want to join file 1 column 3 with file 2 column 4. I would use System.Data.OleDb to read the csv files into a datatable (see http://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp). Then use Linq to join tables. – jdweng Dec 20 '16 at 20:54
  • Please show a small sample input of both files, and the desired output. – Jim Mischel Dec 20 '16 at 21:02

0 Answers0