Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:27:49+00:00 2026-05-27T14:27:49+00:00

So I am creating a list of lines in a text file like this:

  • 0

So I am creating a list of lines in a text file like this:

var lines = File.ReadAllLines("C:\\FileToSearch.txt")
                .Where(x => !x.EndsWith("999999999999"));

and looping through the lines like this

foreach (var line in lines)
{ 
    if (lineCounter == 1)
    {
        outputResults.Add(oData.ToCanadianFormatFileHeader());
    }
    else if (lineCounter == 2)
    {
        outputResults.Add(oData.ToCanadianFormatBatchHeader());
    }
    else
    {
        oData.FromUsLineFormat(line);
        outputResults.Add(oData.ToCanadianLineFormat());

    }
    lineCounter = lineCounter + 1;
    textBuilder += (line + "<br>");
}

Similary like I access the first two rows I would like to access the last and second last row individually

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:27:49+00:00Added an answer on May 27, 2026 at 2:27 pm

    Here you can take advantage of LINQ once again:

    var numberOfLinesToTake = 2;
    var lastTwoLines = lines
         .Skip(Math.Max(0, lines.Count() - numberOfLinesToTake))
         .Take(numberOfLinesToTake);
    
    var secondToLastLine = lastTwoLines.First();
    var lastLine = lastTwoLines.Last();
    

    Or, if you want to retrieve them individually:

    var lastLine = lines.Last();
    var secondToLastLine = 
        lines.Skip(Math.Max(0, lines.Count() - 2)).Take(1).First();
    

    I added .First() to the end, because .Take(1) will return an array containing one item, which we then grab with First(). This can probably be optimized.

    Again, you might want to familiarize yourself with LINQ since it’s a real time-saver sometimes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a list of class Task in a way such as this. List<Task>
Why does this attempt at creating a list of curried functions not work? def
I have to create a list whose list items have 2 lines of text.
I am creating a form with 5 lines of text, and each text I
I'm creating an autocomplete from a page that returnes values as a text list,
EXPLANATION OF THE CODE BELOW: I am loading a .txt file and creating a
I have a text file that looks like: ABC DEF How can I read
I'm trying to create a singly linked list from an input text file for
I have a long list of article titles stored in a text file, and
There is a file lets say List.txt and it contains the information of files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.