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

  • Home
  • SEARCH
  • 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 223491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:13:34+00:00 2026-05-11T19:13:34+00:00

I’m new to LINQ,My knowledge on that library is from Jon Skeet’s book C#

  • 0

I’m new to LINQ,My knowledge on that library is from Jon Skeet’s book “C# In Depth 1”

I read a lot of tutorials about LINQ in the past days including Skeet’s book,but I could never find out a code snippet showing a normal C# code and then the shorter version of that code with LINQ so the reader can understand what does what.

My Problem:
The function below opens a text file and searches for numbers placed at a specific place inside the text file.The numbers(I used ID in the code) start from 1 to 25000 and there are a few that are missing(for example there’s no life 25,but 24,23,26,27 etc).
I want the code to copy the lines in the array “Ids”.Its not meant to be an array,I’m just new and I don’t know if anything else would be more handy for LINQ.

    public static IEnumerable<string> ReadLines(StreamReader reader)
    {
        while (!reader.EndOfStream)
        {
            yield return reader.ReadLine();
        }
    }

    static void Filter(string filename)
    {
        using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
        {
            using (var reader = File.OpenText(filename))
            {
                int[] Ids = { 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660 };
                var myId = from id in Ids
                           from line in ReadLines(reader)
                           let items = line.Split('\t')
                           where items.Length > 1
                           let ItemId = int.Parse(items[1])
                           where ItemId == id
                           select line;
                foreach (var id in myId)
                {
                    writer.WriteLine(id);
                }

            }
        }
    }

What it writes: It only writes one line with number ,which is the first member of the Ids[] array(14652).

I had to add the second ‘from’ ,which is placed at first place in the code so it will check it for every member of the array.I get the from statement as a “while”,because I couldn’t find a snipper with normal code-> linq.

Where is the problem in the code?

  • 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-11T19:13:35+00:00Added an answer on May 11, 2026 at 7:13 pm

    It only writes one line with number ,which is the first member of the Ids[] array(14652).
    Where is the problem in the code?

    from id in Ids
    from line in ReadLines(reader)
    

    This code is conceptually like (ignoring deferred execution):

    foreach(int id in Ids)
    {
      foreach(string line in ReadLines(reader)
      {
        ...
      }
    }
    

    Which will work great for the first id, but then the reader will be at the end of the file for the second id on and no matches will be found.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.