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: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.
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: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
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: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);
}
Source: stackoverflow.com
Related Query
- How to sort & "crop" a dictionary list in C#
- How to Sort a List of Objects by dictionary value?
- How to sort list on multiple properties in one line of code in Linq
- How to sort a dictionary by key, if key is a list (in C#)?
- How to sort a List using a nested Dictionary in Linq C#?
- How can I sort generic list DESC and ASC?
- How to sort a dictionary by key
- How to Sort a list by field
- How to sort a List collection of classes with properties in them?
- C# : How to sort a list of object based on a list of string
- How To Sort A List Of Lists?
- How To Sort A List With Dynamic Objects
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- In c#, how to sort list of doubles by mantissa?
- How do you sort a String List by the number of words matched with an Array in Linq
- How can I sort a list based on a user's selections in ASP.NET MVC?
- How can i sort Generic list with Linq?
- How to sort a list of string?
- How to sort a list compared to an existing list in C#?
- How do I sort a list and its children and their children using linq?
- How to convert Dictionary to List with mixing of values and keys?
- How to sort class list by integer property?
- How to make a SUM of Dictionary Value nested into a list with LINQ?
- How can I sort a list of numerical strings? Preferably using LINQ
- How to sort dictionary by DateTime value
- How can I order a List of Dictionary<int, string> based on Dictionary int value?
- C# how can I sort a list of file by index using linq
- how to sort a list then sort a subset of that list
- How to add items in dictionary in which value is list of class?
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
More Query from same tag
- LINQ: Help with linq query and contains for an IEnumerable<string>?
- Get data by ForeignKey
- Linq many to many query with where clause
- Why does char[].Cast<int> raise a cast exception?
- Getting Empty Row while using Left Join in EF
- Creating XML with LINQ in C#
- Cannot get correct results from Linq query
- How to merge multiple sequences
- LINQ Filtering a List of objects
- Entity Framework + Linq LEFT JOIN using a where clause?
- LINQ: Link 1:M Table and Merge Results
- Get parent objects based on child object field value
- Linq expression to fill model containing other List<T>
- Need to add xml element to last parent using linq c#
- using LINQ to find the cumulative sum of an array of numbers in C#
- Intersect many list by one property
- MVC 4 Retrieve db id from newly created entry
- how to extract common part from a string list using C#
- Linq results type?
- How to alias fields from a DataTable in LINQ?
- Linq OData "Where" clause on nested list
- Query XML element to string
- Checking for item in Generic List before using it
- Query MongoDB 'ObjectId' in C# Linq Driver
- EntityFunction.DiffDays Causes This function can only be invoked from LINQ to Entities
- How to read XML using Linq
- How to call method/function in where clause of a LINQ query as IEnumerable object
- Why doesn't a null cause a NullReferenceException when mapping to and from a null type?
- Filtering results by user name
- How to optmize linq query for grouping dates by price without merging results