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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:46:42+00:00 2026-06-09T20:46:42+00:00

To begin with, I have this inexplicable affection to LINQ and lambda expressions :)

  • 0

To begin with, I have this inexplicable affection to LINQ and lambda expressions 🙂

So I wrote a quite straightforward code using LINQ, which is supposed to get files from a directory according to a certain name pattern, order them and accumulate until the total length of accumulated files exceeds a certain threshold:

IEnumerable<FileInfo> l_allFiles = new DirectoryInfo(l_sDirName).GetFiles().Where(l_fileInfo => ms_pattern.IsMatch(l_fileInfo.Name)).OrderBy(l_fileInfo => l_fileInfo.CreationTime);
int l_nFilesTotal = l_allFiles.Count();
if (nFilesTotal > 0)
{
    long l_nAccumulatedCmdLength = 0;
    IEnumerable<FileInfo> l_selectedFiles = l_allFiles.TakeWhile(l_fileInfo => (l_nAccumulatedCmdLength += l_fileInfo.Length) <= Settings.Default.Threshold);

    int l_nNumOfSelected = l_selectedFiles.Count();
    if (l_nNumOfSelected > 0)
    {
        l_ret = new A { Files = l_selectedFiles };
    }
}

Well, this code works fine when all found files together do not exceed the threshold.
As soon as not all found files are selected to l_selectedFiles, in most cases l_selectedFiles.Count() returns 0 even though l_selectedFiles is not empty.
In rare cases, when l_selectedFiles.Count() returns the correct value, the consecutive call to Files.Count() in the A class returns 0.
To add to the mistery, the debugger always updates the value of l_nAccumulatedLength not after execution of the TakeWhile() method, but after executing the next l_selectedFiles.Count() statement. In cases that not all files are selected, the value of l_nAccumulatedLength is not always as expected…
To complete the picture, I use Microsoft Visual Studio 2010 Ultimate on Windows 7 Professional and my project targets to .NET4.0.

Can anybody give an explanation or a hint to this behavior and/or how to fix it? Because I am quite lost and cannot even imagine how exactly I debug and resolve this issue, and the community is my last hope.

Thank you all in advance for your replies and comments.

  • 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-09T20:46:44+00:00Added an answer on June 9, 2026 at 8:46 pm

    You’re modifying a variable within a query:

    long l_nAccumulatedCmdLength = 0;
    IEnumerable<FileInfo> l_selectedFiles = l_allFiles
         .TakeWhile(l_fileInfo => (l_nAccumulatedCmdLength += l_fileInfo.Length) <=
                                       Settings.Default.Threshold);
    

    Note the change to l_nAccumulatedCmdLength within your TakeWhile condition.

    That’s a really bad idea, which will end up with the sequence giving different results each time you evaluate it. Just don’t do it. I strongly suspect that’s the cause of the problem.

    Note that this part:

    To add to the mistery, the debugger always updates the value of l_nAccumulatedLength not after execution of the TakeWhile() method, but after executing the next l_selectedFiles.Count() statement.

    … is very easily explained. TakeWhile doesn’t iterate over the sequence – it just builds a new sequence which will be lazily evaluated.

    If you want to get consistent results, use ToList… but it would be much better not to modify a variable in the query in the first place. Use Aggregate to create a sequence of Tuple<FileInfo, long> values where the long value is the “size so far” if you want.

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

Sidebar

Related Questions

I have this piece of code that finds words that begin with @ or
I have this code: def paintEvent(self, paintEvent): self._painter.begin(self) try: while True: color, rectangle =
I have this code: But it dosn't work. from viewtopic_body.html <!-- BEGIN custom_fields -->
I'm begin to learn php by my self, for example I have this code,suppose
I have this piece of code: begin complete_results = Timeout.timeout(4) do results = platform.search(artist,
I have this code: int main() { vector<int> res; res.push_back(1); vector<int>::iterator it = res.begin();
I have this code: set<int>::iterator new_end = set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), set1.begin()); set1.erase(new_end, set1.end);
I have this piece of code in a stored procedure: BEGIN SET @UserId =
I have this code... void drawMap(void) { if (false) return; for(auto iter = this->m_layers.begin();
I have this code if filename begin if filename == '-' ARGF.each{|url| begin check(url)

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.