0

I got two .csv files as shown below:

1st file:

"id"
4
1
3

2nd file:

"id"
1
2
3
4
5

I would like to check whether in first file there are all values from second file (no matter in which row they are). In my example in 1st file 2 and 5 values missing comparing to 2nd file so i would like to output somehow those values. How can i do that?

Henry
  • 537
  • 1
  • 9
  • 22
  • Does this answer your question? [How to compare 2 List objects to get the missing value from the List](https://stackoverflow.com/questions/33763942/how-to-compare-2-liststring-objects-to-get-the-missing-value-from-the-liststr) – Drag and Drop Mar 01 '21 at 10:39
  • @DragandDrop and how to load it to lisdt firstly? – Henry Mar 01 '21 at 10:40
  • [How best to read a File into List](https://stackoverflow.com/questions/183791/how-would-you-do-a-not-in-query-with-linq) – Drag and Drop Mar 01 '21 at 10:40
  • Key word for find those where "not in" or "except" for the first link and "File to list string" for the second. – Drag and Drop Mar 01 '21 at 10:41
  • Load your CSV using [CsvHelper](https://joshclose.github.io/CsvHelper/) for example, and then you can work with your record lists as usual to perform comparison between them. – XavierAM Mar 01 '21 at 10:43
  • Question: Do you have a complete file and an incomplete one ? or Can both file be missing value? – Drag and Drop Mar 01 '21 at 10:46

1 Answers1

2

So you know how to read them and don't know how to find out which are missing? You can use Enumerable.Except

IEnumerable<string> firstFileIds = GetFileIds(...);
IEnumerable<string> secondFileIds = GetFileIds(...);
IEnumerable<string> missingInFirst = secondFileIds.Except(firstFileIds);

Console.Write($"Missing in 1st file: {string.Join(",", missingInFirst)}");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Error: cannot convert from 'System.Collections.Generic.IEnumerable' to 'char' – Henry Mar 01 '21 at 10:51
  • Just additional question, do you know how to list duplicates out first list? – Henry Mar 01 '21 at 11:46
  • @Henry: `var dups=firstFileIds.GroupBy(id=>id).Where(g=>g.Count()>1).Select(g=>g.Key);`. Instead of `g.Count()>1` you can also use `g.Skip(1).Any()` since that could be more efficient – Tim Schmelter Mar 01 '21 at 11:48