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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:49+00:00 2026-05-24T20:26:49+00:00

I need to get an element from an IEnumerable and then return itself and

  • 0

I need to get an element from an IEnumerable and then return itself and a range of elements on either side.

So, something like this:

var enumerable = new[] {54, 107, 24, 223, 134, 65, 36, 7342, 812, 96, 106};
var rangeSize = 2;
var range = enumerable.MySelectRange(x => x == 134, rangeSize);

would return something like { 24, 223, 134, 65, 36 }.

(This project uses .Net 3.5)

EDIT
Ok, people seem to be getting hung up on the array of ints.
I’ve changed the example to hopefully make it more clear what I’m after.

Bear in mind that this isn’t necessarily for an IEnumerable<int>, but will actually be an IEnumerable<TSomething>.

  • 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-24T20:26:51+00:00Added an answer on May 24, 2026 at 8:26 pm

    This extension method finds the first element in the sequence satisfying a given predicate, and then returns that element along with a certain number of its neighbouring elements. It handles the end cases.

    public static IEnumerable<T> FirstAndNeighbours<T>(
      this IEnumerable<T> source,
      Func<T,bool> predicate,
      int numOfNeighboursEitherSide)
    {
      using (var enumerator = source.GetEnumerator())
      {
        var precedingNeighbours = new Queue<T>(numOfNeighboursEitherSide);
        while(enumerator.MoveNext())
        {
          var current = enumerator.Current;
          if (predicate(current))
          {
            //We have found the first matching element. First, we must return
            //the preceding neighbours.
            foreach (var precedingNeighbour in precedingNeighbours)
              yield return precedingNeighbour;
    
            //Next, return the matching element.
            yield return current;
    
            //Finally, return the succeeding neighbours.
            for (int i = 0; i < numOfNeighboursEitherSide; ++i)
            {
              if (!enumerator.MoveNext())
                yield break;
    
              yield return enumerator.Current;
            }
            yield break;
          }
          //No match yet, keep track of this preceding neighbour.
          if (precedingNeighbours.Count >= numOfNeighboursEitherSide)
            precedingNeighbours.Dequeue();
          precedingNeighbours.Enqueue(current);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you get the first element from an array like this: var ary
I need to be able get a single specific attribute from an element with
I need to get the first element from a form, could be a select,
I have this pattern where I need to select any random element from a
I need to get element from canvas by given point. For example I have
I need to get neighborhood element value. HTML is <div> <input type='hidden' value='12345'> <div
I need to get the td element of a table. I do not have
i need to get number of order highlighted element (by javascript, jquery): <li>A</li> <li>B</li>
I need to get height of an element that is within a div that
I need a function to get the height of the highest element inside a

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.