Note that there are some explanatory texts on larger screens.

plurals
  1. POCount() returns incorrect result on IEnumerable<T> filled using LINQ
    primarykey
    data
    text
    <p>To begin with, I have this inexplicable affection to LINQ and lambda expressions :) <br/> 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:</p> <pre><code>IEnumerable&lt;FileInfo&gt; l_allFiles = new DirectoryInfo(l_sDirName).GetFiles().Where(l_fileInfo =&gt; ms_pattern.IsMatch(l_fileInfo.Name)).OrderBy(l_fileInfo =&gt; l_fileInfo.CreationTime); int l_nFilesTotal = l_allFiles.Count(); if (nFilesTotal &gt; 0) { long l_nAccumulatedCmdLength = 0; IEnumerable&lt;FileInfo&gt; l_selectedFiles = l_allFiles.TakeWhile(l_fileInfo =&gt; (l_nAccumulatedCmdLength += l_fileInfo.Length) &lt;= Settings.Default.Threshold); int l_nNumOfSelected = l_selectedFiles.Count(); if (l_nNumOfSelected &gt; 0) { l_ret = new A { Files = l_selectedFiles }; } } </code></pre> <p><br/> Well, this code works fine when all found files together do not exceed the threshold.<br/> As soon as not all found files are selected to <code>l_selectedFiles</code>, in most cases <code>l_selectedFiles.Count()</code> returns <strong>0</strong> even though <code>l_selectedFiles</code> is not empty. In rare cases, when <code>l_selectedFiles.Count()</code> returns the correct value, the consecutive call to <code>Files.Count()</code> in the <em>A</em> class returns <strong>0</strong>.<br/> To add to the mistery, the debugger always updates the value of <code>l_nAccumulatedLength</code> not after execution of the <code>TakeWhile()</code> method, but after executing the next <code>l_selectedFiles.Count()</code> statement. In cases that not all files are selected, the value of <code>l_nAccumulatedLength</code> is not always as expected...<br/> To complete the picture, I use <em>Microsoft Visual Studio 2010 Ultimate</em> on <em>Windows 7 Professional</em> and my project targets to <em>.NET4.0</em>.</p> <p>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.</p> <p>Thank you all in advance for your replies and comments.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload