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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:59:31+00:00 2026-06-02T17:59:31+00:00

Using Linq; how can I do the opposite of Take? I.e. instead of getting

  • 0

Using Linq; how can I do the “opposite” of Take?

I.e. instead of getting the first n elements such as in

aCollection.Take(n)

I want to get everything but the last n elements. Something like

aCollection.Leave(n)

(Don’t ask why 🙂

Edit

I suppose I can do it this way aCollection.TakeWhile((x, index) => index < aCollection.Count - n) Or in the form of an extension

public static IEnumerable<TSource> Leave<TSource>(this IEnumerable<TSource> source, int n) 
{ 
  return source.TakeWhile((x, index) => index < source.Count() - n); 
}

But in the case of Linq to SQL or NHibernate Linq it would have been nice if the generated SQL took care of it and generated something like (for SQL Server/T-SQL)

SELECT TOP(SELECT COUNT(*) -@n FROM ATable) * FROM ATable Or some other more clever SQL implementation.

I suppose there is nothing like it?
(But the edit was actually not part of the question.)

  • 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-06-02T17:59:33+00:00Added an answer on June 2, 2026 at 5:59 pm
    aCollection.Take(aCollection.Count() - n);
    

    EDIT: Just as a piece of interesting information which came up in the comments – you may think that the IEnumerable‘s extension method .Count() is slow, because it would iterate through all elements. But in case the actual object implements ICollection or ICollection<T>, it will just use the .Count property which should be O(1). So performance will not suffer in that case.

    You can see the source code of IEnumerable.Count() at TypeDescriptor.net.

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

Sidebar

Related Questions

Using LINQ can I get the data from the table into the format shown
Trying to read an RSS and select information using Linq but can't seem to
How can I get the Nth row using Linq? both columns are text so
Using LINQ, how can I get the column names of a table? C# 3.0,
After I do an insert using linq to sql, can I get the Identity_scope
How can I return first 100 records using Linq? I have a table with
Using LINQ I can find matching elements between two collections like this: var alpha
Using Linq, how can I get a list of objects where their ordinal poistion
Here's my example XML. How can I group these Department elements using LINQ to
Using LINQ or the old-fashioned approach, how can I get all permutations of 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.