score:5
Linq queries are evaluated lazily. Your statement where you assign _listTopN
doesn't perform any work, it only prepares the query ; this query will only be executed when you start enumerating the results. Since you clear matches
before you start enumerating the query, there is nothing in the source to enumerate...
If you want the query to be evaluated eagerly, add ToList
at the end:
var _listTopN = matches.OrderByDescending(s => s.Value)
.Take(Settings.requiredMatch)
.ToList();
Alternatively, you can use ToDictionary
to perform all the work in a single query:
matches = matches.OrderBy(s => s.Value)
.Take(Settings.requiredMatch)
.ToDictionary(s => s.Key, s => s.Value);
score:2
That is because _listTopN
is holding on to a deferred query, lazily executed when you loop on it.
In other words, var _listTopN = ...
does not evaluate the query, it returns a recipe for how to evaluate it when needed.
Since you clear the underlying source before you evaluate it, it'll "change", that is, return something other than what you wanted/expected.
The simple fix is to force evaluation, so do this:
var _listTopN = matches.OrderByDescending(s => s.Value)
.Take(Settings.requiredMatch).ToArray();
^--------^ <-- add this
This evaluates your query and stores the result as an array that won't change, and now you can safely clear your underlying data source.
score:3
Force it to evaluate earlier:
var _listTopN = matches.OrderByDescending(s => s.Value)
.Take(Settings.requiredMatch).ToList();
LINQ has deferred execution (as part of composition); in most cases, nothing is actually evaluated until you foreach
over the data. The ToList()
makes this happen earlier, i.e. before you clear the dictionary.
score:2
Your LINQ expression _listTopN
is lazily evaluated which means that it doesn't evaluate its result until your foreach
statement. But at this point you have already cleared matches
source, so you get nothing in _listTopN
also. You can force LINQ methods to evaluate their results by calling ToArray
for example.
var _listTopN = matches.OrderByDescending(s => s.Value).Take(Settings.requiredMatch)
.ToArray();
See this statement in MSDN
This method is implemented by using deferred execution
score:3
You can assign the result to matches
with ToDictionary
:
if (matches.Count > Settings.requiredMatch)
{
matches = matches
.OrderByDescending(s => s.Value)
.Take(Settings.requiredMatch)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
score:1
if (matches.Count > Settings.requiredMatch)
matches = matches.OrderByDescending(s => s.Value)
.Take(Settings.requiredMatch)
.ToDictionary(s => s.Key, s => s.Value);
Gets matches
into the state you want simply and clearly.
Source: stackoverflow.com
Related Articles
- How to sort & "crop" a dictionary list in C#
- How to Sort a List of Objects by dictionary value?
- Sort on list of objects that contains dictionary
- Sort Dictionary by key then add all values to list
- Sort List with two parameters but in different lines of code
- How to sort list on multiple properties in one line of code in Linq
- Sort Dictionary based on values in a list of integers
- How to sort a dictionary by key, if key is a list (in C#)?
- List or Array of String Contain specific word in Html Source Code
- How to sort a List using a nested Dictionary in Linq C#?
- I want to sort this list in .NET my sample code is below
- c# Linq or code to extract groups from a single list of source data
- Sort a list from another list IDs
- Convert list to dictionary using linq and not worrying about duplicates
- How can I sort generic list DESC and ASC?
- LINQ - Convert List to Dictionary with Value as List
- Create a dictionary on a list with grouping
- Sort a list alphabetically
- Sort list in C# with LINQ
- Sort one list by another
- How to sort a dictionary by key
- Convert dictionary values to list using linq
- List sort based on another list
- LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
- How to Sort a list by field
- LINQ sort a flat list based on childorder
- Sort List except one entry with LINQ
- Sort a List so a specific value ends up on top
- C# sort dictionary with linq
- LINQ method to sort a list based on a bigger list
- LINQ with group by and selecting columns from other table: "not defined in this context" error
- How to write the SQL ORDER BY {value} DESC LIMIT statement in LINQ to Entities (Entity Framewok)?
- join using linq getting error
- SQL order by [Order] but group similar items together
- query for select value in xelement
- Type of one of the expressions in the join clause is incorrect
- How to get the generated Lucene query from a LINQ query
- How to use a conditional or in linq and C#
- How can I convert my query featuring RowNumber() to LINQ?
- Selecting with linq from 2 different objects
- querying from cached data, simple linq statement
- Sort a list of database fetched from MongoDB
- Dynamic Linq Library can’t handling duplicate alias column names
- Average Certain Number Of Elements in List
- Linq - Referencing a Child Table
- how to read the last inserted field with linq
- Take all items except the last ones that satisfy condition?
- LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
- What is the simplest Linq query to produce an N-length array of distinct IDs taken at random?
- Are these two linq queries of the same performance? And How to implement .Any in linq query?