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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:45:13+00:00 2026-05-17T17:45:13+00:00

Good morning, I wrote the following LINQ query public static Func<String, IEnumerable<String>> ListaCorreccoes =

  • 0

Good morning,

I wrote the following LINQ query

 public static Func<String, IEnumerable<String>> ListaCorreccoes = (StrPalavra) =>
    {

        return (from StrTmp in ListaPossiveisCorreccoes(StrPalavra)
                from StrTmp2 in ListaPossiveisCorreccoes(StrTmp)
                where PalavraConhecida(StrTmp2)
                select StrTmp2).Distinct();            

    };

which, to my surprise, is much faster than using two foreach cicles. After running this function using ListaTmp = ListaCorreccoes("comutador");, where ListaTmp is of type IEnumerable<String>, I need to print ListaTmp‘s cardinality using

        Console.Write(">> Prima qualquer tecla para listar todas as " + ListaTmp.Count() + " correcções...");

and to print the contents of ListaTmp, using

foreach (String StrTmp in ListaTmp)
            Console.WriteLine(StrTmp);

However, both the last and prior-to-last lines of code cause ListaTmp and thus the query to be re-evaluated, which is so very strange since the variable is being assigned the result of the query. Why has this code such a strange behaviour?

Thank you very much.

  • 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-17T17:45:14+00:00Added an answer on May 17, 2026 at 5:45 pm

    That’s because LINQ uses deferred execution. See Charlie Calvert’s article on LINQ and Deferred Execution.

    Try this instead:

    var ListaTmp = ListaCorreccoes("comutador").ToList();
    

    This enumerates once and stores the result in a List<string>.

    You might find Jon Skeet’s articles useful:

    • Iterators, iterator blocks and data pipelines
    • Iterator block implementation details: auto-generated state machines
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning ! What is the best way to remove duplicate records from grid
Good Sunday morning! After fiddling around with some JavaScript this weekend, I finally wrote
Good morning, Is it possible for MapViewOfFile to return the same address twice? However,
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Good Morning! Given: public class FooClass { public void FooMethod() { using (var myEntity
Good Early Morning, I have the following python regex file that we established on
Good morning, I am having trouble with the following code <html> <head> <title>highlight date</title>
Good Morning, I wrote the code block below on my local Windows 7 PC
Good morning everbody, I'm trying to pass a UiBinder from a rpc-service to the
Good morning, Suppose I have a class public class Class { int something; int[]

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.