I have two lines of code, one is
AllItems().Where(c => c.Id== id)
.Select(d => new Quality(d.QualityType)).ToList();
and the other one
AllItems().Where(c => c.Id== id).ToList()
.Select(d => new Quality(d.QualityType)).ToList();
The only difference is on the second statement ToList()
is called after the Where
statement. The second statment works just fine.
On the first statement, the default parameterless constructor is hit instead of the constructor with the parameter. so the list is created but the objects in the list are initialized with default values rather than with the d.QualityType.
you can see the full source of the file in question at (Method: GetBestQualityInHistory)
https://github.com/kayone/NzbDrone/blob/master/NzbDrone.Core/Providers/HistoryProvider.cs
**Edit: After further investigation, this seems to be a SubSonic bug, if the Last ToList
is replaced by an OrderBy
subsonic throws an The construtor 'Void .ctor(NzbDrone.Core.Repository.Quality.QualityTypes, Boolean)' is not supported
.
If SubSonic works in the same way as Entity framework you cannot use constructors with parameters - you must use parameterless constructors and initializers. My very high level explanation of this is that the query is not executed as is - it is translated to SQL and because of that you must use property initializers so that expression tree knows which properties in the projected type should be filled by values. When using constructor with parameters, expression tree doesn't know where the passed parameter belongs to (it doesn't check content of the constructor). The real constructor (parameterless) is called once you execute
Tolist
and result set is materialized intoQuantityType
instances.