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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:17:50+00:00 2026-05-15T06:17:50+00:00

consider the following example: public IEnumerable<String> Test () { IEnumerable<String> lexicalStrings = new List<String>

  • 0

consider the following example:

    public IEnumerable<String> Test ()
    {
        IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
        IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };

        IEnumerable<String> lexicals = new List<String> ();
        foreach (String s in lexicalStrings)
            lexicals = lexicals.Union (allLexicals.Where (lexical => lexical == s));

        return lexicals;
    }

I’d hoped for it to produce “test”, “t” as output, but it does not (The output is only “t”). I’m not sure, but may have to do something with the deferred processing. Any ideas how to get this to work or for a good alternative?

Edit: Please note that this is just a simplified example. lexicalStrings and allLexicals are different types in the original code. So I cannot directly combine these.

Edit2 the problem to solve looks more like this:

    public IEnumerable<Lexical> Test ()
    {
        IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
        IEnumerable<Lexical> allLexicals = new List<Lexical> { ... };

        IEnumerable<Lexical> lexicals = new List<Lexical> ();
        foreach (String s in lexicalStrings)
            lexicals = lexicals.Union (allLexicals.Where (lexical => lexical.Text == s));

        return lexicals;
    }
  • 1 1 Answer
  • 3 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-15T06:17:51+00:00Added an answer on May 15, 2026 at 6:17 am

    You are using wrong operation as other answer explaining. But still it is interesting why your code works incorrectly despite looking fine.

    let’s modify your app a bit:

            IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
            IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };
    
            IEnumerable<String> lexicals = new List<String>();
            foreach (String s in lexicalStrings)
            {
                lexicals = lexicals.Union(
                    allLexicals.Where(
                    lexical =>
                    {
                        Console.WriteLine(s);
                        return lexical == s;
                    }
                    )
                );
            }
            Console.WriteLine();
            foreach (var item in lexicals)
            {
            }
    

    what output do you expect? here is it:

    t
    t
    t
    t
    t
    t
    t
    t
    

    interesting, is not it?

    now let’s modify it again:

        IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
        IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };
    
        IEnumerable<String> lexicals = new List<String>();
        foreach (String s in lexicalStrings)
        {
            string ls = s;
            lexicals = lexicals.Union(
                allLexicals.Where(
                lexical =>
                {
                    Console.WriteLine(ls);
                    return lexical == ls;
                }
                )
            );
        }            
        foreach (var item in lexicals)
        {                
        }
    

    now the output and results are fine:

    test
    test
    test
    test
    t
    t
    t
    t
    

    Why does it happen? You use closure – the use of outer var in inner lambda. Since you do not actually iterate your sequence the current value of s doesn’t get into the lambda. foreach exits and all inner copies of s hold value of last iteration. In case of inner variable they hold values copies that are created for every iteration. This conflict comes from inner lazyness of LINQ. If you do things like List.AddRange inside loop result will be fine, because List.AddRange forces iteration.

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

Sidebar

Related Questions

Consider the following example: interface IPropertyCollection { public MethodWrapper GetPropertySetterByName(string name); //<<-- I want
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following example: public class BaseClass { public string StringInBaseClass {get;set;} public int
Consider the following method example: public void MyMethod (string par1, bool par2 = true,
Consider the following method example: public static string[] ParseOptions() { return Environment.GetCommandLineArgs(); } What
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider the following example $q = Doctrine::getTable('User')->createQuery('u') ->where('u.username = ?', 'test'); If another related
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider the following example: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html xmlns=http://www.w3.org/1999/xhtml
Consider the following simple RESTEasy (JAX-RS) service: @Path(/example-service) public interface ExampleService { @Path(/ping) @GET

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.