score:3
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.
Source: stackoverflow.com
Related Query
- Count() returns incorrect result on IEnumerable<T> filled using LINQ
- Linq Query Returns Incorrect Result Set
- Retrieve bool result by using LinQ code
- Linq query using ID returns result to slow (EF Core)
- Using LINQ query result for data source for GridControl c#
- Take result with max count using LINQ
- Filter result using linq to return a count of entries by remove duplicates using specific parameter
- Convert string[] to int[] in one line of code using LINQ
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Get item count of a list<> using Linq
- PagedList using LINQ Skip and Take, but show paging using Count of results
- How do I count the number of child collection's items using LINQ Method Syntax?
- linq where clause and count result in null exception
- Linq to SQL using group By, and order by count
- Count values in Dictionary using LINQ and LINQ extensions
- Count Occurrences of an Item in a List using LINQ
- Using Linq on a Client Object model result from sharepoint
- Using LINQ to generate a random size collection filled with random numbers
- Using LINQ to count value frequency
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Get Value and Count of that value using LINQ or lambda expression
- Return List with Maximum Count using Linq
- C# concat two Collection<string> using linq and getting a Collection<string> result
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- How-to ensure multiple collections have the same item count using LINQ
- LINQ Source Code Available
- Count distinct values of a column in dataGridView using linq in .NET
- Count the number of children in my JSON file using JSON.NET with LINQ
More Query from same tag
- How to generate SQL COUNT(*) OVER (PARTITION BY {ColumnName}) in LINQ-to-SQL?
- how to assign linq result to multidimensional array
- Can Regex remove multiple surrounding {"TextWithHtml": and } in a string?
- How can you order a hierarchy of objects by depth-level using LINQ?
- Error - Corresponding column type must be compatible - DateTime field - Entity Framework, Informix, Load multiple childs
- How to remove duplicates from a predicate object?
- display data form multiple tables in a strongly typed view
- Linq does not recognize method GetDistanceTo
- Why can I leave a parameter off a LINQ condition?
- Calculating difference between different columns in different rows
- How to get the items with lowest value using LINQ?
- get keys of KeyValuePair List to int[] array using linq
- Why is this LINQ query not returning the correct dates?
- SQL query shows good database values, but LINQ to Entity framework brings a null value from nowhere
- C# Linq convert collection using groupby
- Convert SQL query with an "In" condition to Lambda expression
- I have an IQueryable with WHERE IN (ids). Can ToList() return the same order as ids?
- Linq Join Duplicates records c#
- GroupBy and OrderBy using LINQ
- Building Xml with XElement dynamically through recursion
- Linq .Sum() function fails when there is nothing to Sum
- Entity Framework Relationships Foreach
- Linq filter by several enumerators
- select new in linq c#
- LINQ - What does (x, i) do?
- Join multiple tables and return last record from table 2
- Get all subordinates who directly or indirectly reports to supervisor with parameter supervisor
- Tie Entity Framework calls to a common parameter such as UserId
- Using LINQ to get DataGridView row index where first column has specific value
- How can I use lazy loading logic of pagination to retrieve FileSystem kind of hierarchichal data