C# Matching an item in a list with the corresponding item in a text file

Hello experts
C#
I have a List Lnodes with just one item per line: an int value  named "IdNumber"

I have a .txt file with two items per line:
- "IdNumber" (always the same lenght and position)
- a separator (;)
- "Position" (a string item with different length)

I need to read, line by line, the List Lnodes and,  for each value of IdNumber;
             - browse the .txt file
             - find the line that contains the same IdNumber value
             - extract on that line the corresponding "Position" string
             - Add that value to a row on another list

I'v tried a lot of methods (substrings, arrays, and so on) and now I'm lost !!
Hereunder are a sample of Lnodes and another of the .text file


List Lnodes

310
311
312
313
314
....



.txt file
310;UpSpine
311;LeftArm
312;LeftForeArm
313;LeftHand
314;MidSpine
315;RightForeArm
316;RightArm
317;Pelvis
PaulagierResearch DirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
The best way is to split it based on the delimiter . . .eg the ;.

string line;
int NodeNumber;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\myFile.txt");
while((line = file.ReadLine()) != null)
{
NodeNumber = -1;
string  firstPart = line.Split(new char[]{';'})[0];
  if (int.TryParse(firstPart, out NodeNumber))
  {
   // will only happen if firstPart is parsed
   if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    } 
   }
}   

Open in new window

PaulagierResearch DirectorAuthor Commented:
Hello Kyle,
it seems not to be exactly what I'm looking for: i need to read first the List and see if its IdNumber value has an equivalent in the .txt files.
In the solution you wrote, if a NodeNumber does not match with the text file I can't know it
regards
Fernando SotoRetiredCommented:
Hi Paulagier;

Try this out, it should give you what you need.
// File location on disk
string filename = @"C:\Working Directory\data.txt";
// You list of Lnodes
List<int> Lnodes = new List<int>() { 317, 311, 312, 313, 314, 315, 316, 310 };
// Read the text file into memory
List<string> txtFile = File.ReadAllLines(filename).ToList();
// All the position data is in this List in the same order as Lnodes
List<string> position;

position = (from id in Lnodes
            join pos in txtFile on id.ToString() equals pos.Substring(0, 3)
            select pos.Substring(4)).ToList();

Open in new window

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Kyle AbrahamsSenior .Net DeveloperCommented:
can you write the full example of what your'e trying to do . . . you can know it by adding an else but your requirements aren't clear to me.

  if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    }
  else
   {
   //Not in LNOdes . . . do something else?
  }
PaulagierResearch DirectorAuthor Commented:
if (Lnodes.Contains(NodeNumber))
   {
    //Lnodes has this number, do something.
    }
  else
   {
   //Not in LNOdes . . . do something else?
  }

The problem comes if an IdNumber value is contained in LNodes and NOT in the .txt file. It seems to me that in your solution i cannot handle that situation.
Let's add in my example the LNodes value "321" , everything else equal.
Fernando SotoRetiredCommented:
What do you need to happen when a Lnodes value is NOT in the text file?

Did you try my solution?
PaulagierResearch DirectorAuthor Commented:
Dear Fernando
I nned to store somewhere (list, .txt file..)  the IdNumber that is not in the text file
Fernando SotoRetiredCommented:
So are you saying that you only need the Lnodes numbers  that are not in the text file and not a list of a position that are in the file?

If that is not the case, given the two input info, List of Lnodes and the text file, please show with values what you want the resulting object should look like.
Kyle AbrahamsSenior .Net DeveloperCommented:
where are you storing them?

if (!Lnodes.Contains(NodeNumber))
   {
 
    //store the NodeNumber
  }
PaulagierResearch DirectorAuthor Commented:
List Lnodes            .txt file
310                           310;UpSpine
311                           311;LeftArm
312                          312;LeftForeArm
313                          313;LeftHand
314                          314;MidSpine
320                          315;RightForeArm
                           316;RightArm
                           317;Pelvis

I need the three subsets:
In LNodes and in .txt file:                 310,311,312,313,314
In LNodes and NOT in .txt file:        320
In .txt file and NOT in vNode:          315,316,317
Fernando SotoRetiredCommented:
Here you go Paulagier as per your most recent specifications.
// File location on disk
string filename = @"C:\Working Directory\data.txt";
// You list of Lnodes
List<int> Lnodes = new List<int>() { 310, 311, 312, 313, 314, 320 };
// Read the text file into memory
List<string> txtFile = File.ReadAllLines(filename).ToList();
// Three result sets 1: In LNodes and in .txt file, 2: In LNodes and NOT in .txt file and 3: In .txt file and NOT in vNode
List<string> inLnodesAndFile;
List<string> inLnodesNotFile;
List<string> inFileNotLnodes;

// Redefined the input to be in the same data type and cut down on typing
List<string> LnAsString = Lnodes.Select(l => l.ToString()).ToList();
List<string> fileIds = txtFile.Select(fi => fi.Substring(0, 3)).ToList();

// The requested data
inLnodesAndFile = LnAsString.Intersect(fileIds).ToList();
inLnodesNotFile = LnAsString.Except(fileIds).ToList();
inFileNotLnodes = fileIds.Except(LnAsString).ToList();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulagierResearch DirectorAuthor Commented:
A GREAT solution,  with a LOT of expertise-behind
Thanks Fernando
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.