score:10
Because you're typecasting from an int to double, it's not a conversion it's a type cast and this is somewhat different from when you cast int to double in the general case.
The Cast<T>
extension method uses the IL instruction unbox.any
While a C# cast like this
var x = (double)42;
Actually results in the IL instruction
conv.r8
Fundamentally unboxing an type as a different type is wrong and that's why you get the exception.
This question has the same answer and also links to a blog post by Eric Lippert.
score:6
list
contains ints
, which you attempt to cast to double
. Change your query to
var query = list.Select(d => (double)d);
Update
Here's the source for Enumerable.Cast
(.NET 4):
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source) {
IEnumerable<TResult> typedSource = source as IEnumerable<TResult>;
if (typedSource != null) return typedSource;
if (source == null) throw Error.ArgumentNull("source");
return CastIterator<TResult>(source);
}
static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source) {
foreach (object obj in source) yield return (TResult)obj;
}
As you can see, the CastIterator
attempts to cast an object
(which in this case is a boxed int
) to a double
. Unboxing operations only succeed if the target type is exactly the same as the original type that was boxed, so an exception is thrown. This link that John Leidegren provided explains in detail.
score:2
There is a difference between a .NET Framework type-cast and a C# type conversion. They are NOT the same. "Casting" int to double is a feature of the C# language, and of the language alone.
The C# compiler can convert an int to double using a special instruction for this purpose. The compiler knows, at compile time, that the source type is "int", the destination type is "double", and therefore can generate the proper instruction. This is not really a type cast in the .NET Framework sense.
System.Int32 cannot be type-cast to System.Double. The Cast extension method was compiled without knowing the exact types of the source and the destination collections, and therefore no special instructions for handling C# specific features were generated. The only code that is generated for the Cast extension method is a normal .NET type cast (casting classes to their base types, casting to object, and casting types to the interfaces they implement).
Source: stackoverflow.com
Related Articles
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF code first - getting DynamicProxies instead of objects. Why?
- Why am I getting an InvalidCastException when using an expression from a static field?
- creating Linq to sqlite dbml from DbLinq source code
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- Getting "Could not find an implementation of the query pattern for source type 'ExcelQueryable<T>'. " Error
- Using var anonymous object getting InvalidCastException for LINQ
- ASP.net Getting Max Date of database Date column How to avoid Null in Date Column C# My code Attached
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Getting the Error in my code when framing LINQ
- Getting Value cannot be null. parameter name: source on release but not in debug
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Getting keys from a Lookup
- Is there a LINQ function for getting the longest string in a list of strings?
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- C# XML find first Element descendants
- Get all elements that only occur once
- Extend DbFunctions EF6
- Merging duplicate strings in List and summing them
- Linq query syntax to method query syntax
- Why can the anonymous type be changed if it is supposed to be immutable?
- What would be a good way to add "friendly" column names to a LINQ to SQL model?
- How do I fill ONE View from Separate Tables using Linq?
- Retrieving property from first item with LINQ
- Converting Collection of Strings to Dictionary
- Linq Join with Distinct
- Fill List<int> using LINQ
- Is there any way to encapsulate a simple LINQ query in an extension method that can be used with LINQ to Entities query?
- Group by clause inside a nested list
- Linq Get count from multiple where condition
- Select Numeric Values From String Linq to Entities
- How to write a linq statement that parses and checks value of a list
- GroupBy a property, make a list of the repeated ones
- In C# can you perform a Range Select within a Range Select?
- LINQ to EF - Simulating SQL "IN" clause