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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:09:18+00:00 2026-06-09T14:09:18+00:00

Given a collection of items, how do I split the collection into 2 sub-collections

  • 0

Given a collection of items, how do I split the collection into 2 sub-collections based on a predicate?

You could do 2 Where searches, but then the run time is 2*N (which, while still O(n), takes twice as long and is obviously not preferred)

IEnumerable<int> even = nums.Where(i => IsEven(i));
IEnumerable<int> odd = nums.Where(i => !IsEven(i));

You could do a single linear pass yourself (refactored into an extension method here), but this means you have to drag this code all over, and more custom code makes things less maintainable.

public static void SplitOnPred<T>(
        this IEnumerable<T> collection,
        Func<T, bool> pred,
        out IEnumerable<T> trueSet,
        out IEnumerable<T> falseSet
    ) {
        List<T> trueSetList = new List<T>();
        List<T> falseSetList = new List<T>();
        foreach( T item in collection ) {
            if( pred( item ) ) {
                trueSetList.Add( item );
            } else {
                falseSetList.Add( item );
            }
        }
        trueSet = trueSetList;
        falseSet = falseSetList;
}

Question:
Does LINQ have any native support for splitting a collection in 1 linear pass?

  • 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-09T14:09:19+00:00Added an answer on June 9, 2026 at 2:09 pm

    Does LINQ have any native support for splitting a collection in 1 linear pass?

    There are no built-in methods that split a collection into two versions based on a predicate. You would need to use your own method, similar to the one you posted.

    The closest built-in method would be GroupBy (or ToLookup). You could group by odd or even:

    var groups = nums.GroupBy(i => IsEven(i));
    

    This will split into two “groups” based on whether the numbers are odd or even.

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

Sidebar

Related Questions

Given: <TextBox Text='{Binding MyCollection[MyIndex].MyProperty}'/> where: MyCollection is an observable collection of MyClass items MyClass
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
I am building a .NET application that given a connection string, at run time,
Given a collection of items { url: 'http://blah' }. How can I tell if
Given a linq expression of an object collection 'items' such as this: var total
I want to databind an ItemsCollection, but instead of rendering the collection items, I
im getting a CFDictionaryAddValue(): immutable collection 0xd5aea0 given to mutating function error when i
Given: Several million records in a mongo collection. Each record has 10 fields, of
Given a .net 4. collection, if I want to pass in my own custom
Given the following XML structure: <root> <data>x</data> <details> <some_other_element>...</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>

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.