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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:53:16+00:00 2026-05-26T03:53:16+00:00

Assume that you have two IEnumerbale objects. How we can merge them (in some

  • 0

Assume that you have two IEnumerbale objects. How we can merge them (in some condition e.g merge in merge sort …) and create a unique IEnumerable? I tried this with Zip, but in Zip the two list sizes should be equal (maybe you didn’t get exception but maybe we have some data lost.)

In addition, I try it by using Enumerable.Range(…).Select(…) but i didn’t get an acceptable result.

Furthermore, my question is totally different from using Union or this one, in fact as I said like merge in merge sort I like to preserve lists order (in fact just want to fill some gaps in first list).

It’s easy to handle it with for loop, but i can’t see any full linq way.

Edit:

Sample input:

lst1 = {5,10,12}
lst2 = {7,9,16,20,25}

result: {5,7,9,10,12,16,20,25}

this can be done with a for loop and two pointer in O(n + m) but I’m looking for linq solution in O(n+m)

for loop solution:

        var lst1 = new List<int> { 5, 10, 12 };
        var lst2 = new List<int> { 7, 9, 16, 20, 25 };

        var result = new List<int>();

        int j = 0;
        for (int i = 0; i < lst1.Count; i++)
        {
            while (j < lst2.Count && lst2[j] < lst1[i])
            {
                result.Add(lst2[j]);
                j++;
            }
            result.Add(lst1[i]);
        }

        while (j < lst2.Count)
        {
            result.Add(lst2[j]);
            j++;
        }
        Console.WriteLine(string.Join(",", result.ToArray()));
  • 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-26T03:53:16+00:00Added an answer on May 26, 2026 at 3:53 am

    There is no such method in LINQ. And I don’t think it’s possible to combine the existing methods to do exactly what you want (if it was, it would be overly complicated).

    But implementing such method yourself isn’t that hard:

    static IEnumerable<T> Merge<T>(this IEnumerable<T> first,
                                   IEnumerable<T> second,
                                   Func<T, T, bool> predicate)
    {
        // validation ommited
    
        using (var firstEnumerator = first.GetEnumerator())
        using (var secondEnumerator = second.GetEnumerator())
        {
            bool firstCond = firstEnumerator.MoveNext();
            bool secondCond = secondEnumerator.MoveNext();
    
            while (firstCond && secondCond)
            {
                if (predicate(firstEnumerator.Current,  secondEnumerator.Current))
                {
                    yield return firstEnumerator.Current;
                    firstCond = firstEnumerator.MoveNext();
                }
                else
                {
                    yield return secondEnumerator.Current;
                    secondCond = secondEnumerator.MoveNext();
                }
            }
    
            while (firstCond)
            {
                yield return firstEnumerator.Current;
                firstCond = firstEnumerator.MoveNext();
            }
    
            while (secondCond)
            {
                yield return secondEnumerator.Current;
                secondCond = secondEnumerator.MoveNext();
            }
        }
    }
    

    And you could use it like this:

    lst1.Merge(lst2, (i, j) => i < j)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have two objects. Furthermore, let's assume that they really have no
I am currently implementing a method that accepts two bitmap objects. We can assume
For example, let's assume that i have two QuerySEt objects: queryset1 = my_model1.objects.all().order_by('-created') queryset2
I have some question about linux-kernel, let's assume that I have two threads in
To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck
I'm trying to create an expression tree dynamicly. Let asume that I have two
Assume that we have multiple arrays of integers. You can consider each array as
Assume that I have two parent tables: Company and Contact . Assume that both
Assume that we have two tables: Roles and Reports . And there exists a
I have two tables say ( FCT_SALES_SUMMARY_A and FCT_SALES_SUMMARY_B ). If we assume that

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.