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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:37:29+00:00 2026-05-13T21:37:29+00:00

Given a list of elements like so: int[] ia = new int[] { -4,

  • 0

Given a list of elements like so:

int[] ia = new int[] { -4, 10, 11, 12, 13, -1, 9, 8, 7, 6, 5, 4, -2, 
                        6, 15, 32, -5, 6, 19, 22 };

Is there an easy way in Linq to do something along the lines of “Select the elements from the -1 up to the next negative number (or the list exhausts)”? A successful result for -1 would be (-1, 9, 8, 7, 6, 5, 4). Using -2 would give the result (-2, 6, 15, 32).

Not a homework problem. I’m just looking at an implementation using a bool, a for loop, and an if wondering if there’s a cleaner way to do it.

  • 1 1 Answer
  • 1 View
  • 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-13T21:37:29+00:00Added an answer on May 13, 2026 at 9:37 pm

    Take a look at the TakeWhile Linq extension method. Takes items from the list as long as the condition is true, skips the rest.

    Example:

    int[] ia = new int[] { -4, 10, 11, 12, 13, -1, 9, 8, 7, 6, 5, 4, -2, 
                            6, 15, 32, -5, 6, 19, 22 };
    
    var result = ia
                 .SkipWhile(i => i != -1)
                 .Skip(1)
                 .TakeWhile(i => i >= 0);
    

    Note the Skip(1) after the SkipWhile. SkipWhile skips everything up to, but not including the matching element. TakeWhile then Takes items, up to but not including the matching element. Because -1 is not greater than or equal to zero, you get an empty result.

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

Sidebar

Related Questions

I can easily write a predicate to get unique elements from a given list
This exercise is taken from Google's Python Class : D. Given a list of
Given two IEnumerable s of the same size, how can I convert it to
The assignment, for my data structures class, is to find the shortest path from
I have a generic list of type Element , for example. public class Element
I know the differences between Set and List(unique vs. duplications allowed, not ordered/ordered, etc).
I know the differences between Set and List(unique vs. duplications allowed, not ordered/ordered, etc).
I know generally two ways to design a generic linked list datastructure in C.
Here is a function I would like to write but am unable to do
Intro to problem: I am given recipes how to craft items. Recipe is in

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.