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 3220110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:43:25+00:00 2026-05-17T15:43:25+00:00

I know, this is very simple for you guys. Please consider the following code:

  • 0

I know, this is very simple for you guys.
Please consider the following code:

string[] str = { "dataReader", "dataTable", "gridView", "textBox", "bool" };

            var s = from n in str
                    where n.StartsWith("data")
                    select n;

            foreach (var x in s)
            {
                Console.WriteLine(x.ToString());
            }
            Console.ReadLine();

Supposedly, it will print:

dataReader
dataTable

right?

What if for example I don’t know the data, and what the results of the query will be (but I’m sure it will return some results) and I just want to print the second item that will be produced by the query, what should my code be instead of using foreach?

Is there something like array-indexing here?

  • 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-17T15:43:26+00:00Added an answer on May 17, 2026 at 3:43 pm

    You’re looking forEnumerable.ElementAt.

    var secondMatch = str.Where(item => item.StartsWith("data")) //consider null-test
                         .ElementAt(1); 
    
    Console.WriteLine(secondMatch); //ToString() is redundant
    

    SinceWherestreams its results, this will be efficient – enumeration of the source sequence will be discontinued after the second match (the one you’re interested in) has been found.

    If you find that the implicit guarantee you have that the source will contain two matches is not valid, you can use ElementAtOrDefault.

    var secondMatch = str.Where(item => item.StartsWith("data"))
                         .ElementAtOrDefault(1); 
    
    if(secondMatch == null) // because default(string) == null
    {
       // There are no matches or just a single match..
    }
    else
    {
      // Second match found..
    }
    

    You could use array-indexing here as you say, but only after you load the results into… an array. This will of course mean that the entire source sequence has to be enumerated and the matches loaded into the array, so it’s a bit of a waste if you are only interested in the second match.

    var secondMatch = str.Where(item => item.StartsWith("data"))
                         .ToArray()[1]; //ElementAt will will work too
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys As you know checking process of web pages content is a little
hi I'm really frustrated, First I have no idea how to code the very
I know this stream works because of two reasons: 1) It works OUTSIDE of
First of all, I know this is a bad solution and I shouldn't be
Hey guys, I do most of my work in MySQL, but have recently been
I've found some questions that seem related to this one, but none describes exaclty
I'm pretty new to iPhone programming, so I'm looking for any pointers (no pun
Problem: In MS Access you can hold the shift key when opening a database
I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish +

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.