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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:39:51+00:00 2026-05-16T23:39:51+00:00

There is the enumerable extension method Take<TSource>( IEnumerable<TSource> source, int count ) which takes

  • 0

There is the enumerable extension method

Take<TSource>(
    IEnumerable<TSource> source,
    int count
)

which takes the first count elements from the start.

Is there a way to take the elements from the end?
or even better a way to take the elements from an offset to the end?

Thanks

  • 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-16T23:39:51+00:00Added an answer on May 16, 2026 at 11:39 pm
    finiteList.Reverse().Take(count).Reverse();
    

    or

    finiteList.Skip(finiteList.Count() - count)
    

    There is some overhead in doing this so a custom method would be better.

    Update: A custom method

    public static class EnumerableExtensions
    {
        public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (count < 0) throw new ArgumentOutOfRangeException("count");
    
            if (count == 0) yield break;
    
            var queue = new Queue<T>(count);
    
            foreach (var t in source)
            {
                if (queue.Count == count) queue.Dequeue();
    
                queue.Enqueue(t);
            }
    
            foreach (var t in queue)
                yield return t;
        }
    }
    

    Update: Changed the code a littlebit with ideas from dtb´s answer 🙂

    Comment to Bear: Look at this example:

    var lastFive = Enumerable.Range(1, 10).TakeLast(5);
    var lastFive2 = Enumerable.Range(1, 10).TakeLast2(5); //Bear´s way
    
    Queue<int> q = (Queue<int>)lastFive2;
    q.Dequeue();
    
    //Is lastFive2 still last five? no...
    

    You could potentially change the values of lastFive2 and therefore that approach can be unsafe or at least it´s not the functional way.

    To Bear:

    What I meant about safe is this:

    var lastFive2 = Enumerable.Range(1, 10).TakeLast2(5); //Bear´s way
    
    //some = Some method which you don't control - it could be from another assembly which represents a crazy plugin etc.
    some(lastFive2);
    //Now what?
    

    In these cases you would have to make a copy to be sure. But in most cases your way would be fine – and a little bit more efficient than this so +1 🙂

    An idea is to use a queue which only have internal Enqueue etc.

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

Sidebar

Related Questions

is there anyway to return the object and not the value from Method: Mongoid::Contexts::Enumerable#max
In the .net framework, there's a generic IEnumerable<T> interface which inherits from the not-generic
Is there any method / extension method on IEnumerable that allows me to find
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
I have an extension method defined like so: public static TSource MaxBy<TSource, TResult>(this IEnumerable<TSource>
Hey, I'm using the Enumerable.Sum() extension method from LINQ to compute hash codes, and
why linq's except extension method does not have Except<TSource> Method (IEnumerable<TSource>,HashSet<TSource>) overload? for example
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
There is some code that I'm trying to convert from IList to IEnumerable :

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.