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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:42:37+00:00 2026-05-26T02:42:37+00:00

Suppose I have a given object of type IEnumerable<string> which is the return value

  • 0

Suppose I have a given object of type IEnumerable<string> which is the return value of method SomeMethod(), and which contains no repeated elements. I would like to be able to “zip” the following lines in a single LINQ query:

IEnumerable<string> someList = SomeMethod();

if (someList.Contains(givenString))
{
    return (someList.Where(givenString));
}
else
{
    return (someList);
}

Edit: I mistakenly used Single instead of First. Corrected now.

I know I can “zip” this by using the ternary operator, but that’s just not the point. I would just list to be able to achieve this with a single line. Is that possible?

  • 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-26T02:42:38+00:00Added an answer on May 26, 2026 at 2:42 am

    The nature of your desired output requires that you either make two requests for the data, like you are now, or buffer the non-matches to return if no matches are found. The later would be especially useful in cases where actually getting the data is a relatively expensive call (eg: database query or WCF service). The buffering method would look like this:

    static IEnumerable<T> AllIfNone<T>(this IEnumerable<T> source, 
                                       Func<T, bool> predicate)
    {
        //argument checking ignored for sample purposes
        var buffer = new List<T>();
        bool foundFirst = false;
        foreach (var item in source)
        {
            if (predicate(item))
            {
                foundFirst = true;
                yield return item;
            }
            else if (!foundFirst)
            {
                buffer.Add(item);
            }
        }
    
        if (!foundFirst)
        {
            foreach (var item in buffer)
            {
                yield return item;
            }
        }
    }
    

    The laziness of this method is either that of Where or ToList depending on if the collection contains a match or not. If it does, you should get execution similar to Where. If not, you will get roughly the execution of calling ToList (with the overhead of all the failed filter checks) and iterating the result.

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

Sidebar

Related Questions

Suppose I have a given Object (a string a, a number - let's say
Suppose I have an object Person , which has_many :foos and :bars . Given
Assuming we have a given object of type Person and want to store its
Suppose I have a the following in Scala object Foo { var functions: List[String
Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
I have recently come across an interesting question on strings. Suppose you are given
I have been given a staff list which is supposed to be up to
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
Suppose I have some code which is running in the UI thread, which spawns

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.