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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:20:15+00:00 2026-05-27T22:20:15+00:00

I have an IEnumerable of a custom type. (That I’ve gotten from a SelectMany)

  • 0

I have an IEnumerable of a custom type. (That I’ve gotten from a SelectMany)

I also have an item (myItem) in that IEnumerable that I desire the previous and next item from the IEnumerable.

Currently, I’m doing the desired like this:

var previousItem = myIEnumerable.Reverse().SkipWhile( 
    i => i.UniqueObjectID != myItem.UniqueObjectID).Skip(1).FirstOrDefault();

I can get the next item by simply ommitting the .Reverse.

or, I could:

int index = myIEnumerable.ToList().FindIndex( 
    i => i.UniqueObjectID == myItem.UniqueObjectID)

and then use .ElementAt(index +/- 1) to get the previous or next item.

  1. Which is better between the two options?
  2. Is there an even better option available?

“Better” includes a combination of performance (memory and speed) and readability; with readability being my primary concern.

  • 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-27T22:20:16+00:00Added an answer on May 27, 2026 at 10:20 pm

    First off

    “Better” includes a combination of performance (memory and speed)

    In general you can’t have both, the rule of thumb is, if you optimise for speed, it’ll cost memory, if you optimise for memory, it’ll cost you speed.

    There is a better option, that performs well on both memory and speed fronts, and can be used in a readable manner (I’m not delighted with the function name, however, FindItemReturningPreviousItemFoundItemAndNextItem is a bit of a mouthful).

    So, it looks like it’s time for a custom find extension method, something like . . .

    public static IEnumerable<T> FindSandwichedItem<T>(this IEnumerable<T> items, Predicate<T> matchFilling)
    {
        if (items == null)
            throw new ArgumentNullException("items");
        if (matchFilling == null)
            throw new ArgumentNullException("matchFilling");
    
        return FindSandwichedItemImpl(items, matchFilling);
    }
    
    private static IEnumerable<T> FindSandwichedItemImpl<T>(IEnumerable<T> items, Predicate<T> matchFilling)
    {
        using(var iter = items.GetEnumerator())
        {
            T previous = default(T);
            while(iter.MoveNext())
            {
                if(matchFilling(iter.Current))
                {
                    yield return previous;
                    yield return iter.Current;
                    if (iter.MoveNext())
                        yield return iter.Current;
                    else
                        yield return default(T);
                    yield break;
                }
                previous = iter.Current;
            }
        }
        // If we get here nothing has been found so return three default values
        yield return default(T); // Previous
        yield return default(T); // Current
        yield return default(T); // Next
    }
    

    You can cache the result of this to a list if you need to refer to the items more than once, but it returns the found item, preceded by the previous item, followed by the following item. e.g.

    var sandwichedItems = myIEnumerable.FindSandwichedItem(item => item.objectId == "MyObjectId").ToList();
    var previousItem = sandwichedItems[0];
    var myItem = sandwichedItems[1];
    var nextItem = sandwichedItems[2];
    

    The defaults to return if it’s the first or last item may need to change depending on your requirements.

    Hope this helps.

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

Sidebar

Related Questions

I have a custom class called Rows that implements IEnumerable<Row> . I often use
I have a custom control Workspace that inherits from Control and within it is
The problem: I have a custom collection PagedList<T> that is being returned from my
I have a model that contains an IEnumerable of a list of custom objects.
Lets say I have a custom data type that looks something like this: public
I have a IEnumerable. I have a custom Interval class which just has two
I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of
I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos. Assuming that I can compare
I have an IEnumerable<DateTime> . I would like to pull out all dates that
If I have an IEnumerable where ClassA exposes an ID property of type long.

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.