I would like to create an extension method to convert a ICollection of type long to a ICOllection of type long?. It would be something like that:
public static void ConvetirLongANullableLong<T, U>(this ICollection<T> paramIcOrigenNoNullable, ICollection<U> paramIcDestinoNullable)
{
for (int i = 0; i < paramIcOrigenNoNullable.Count; i++)
{
paramIcDestinoNullable.Add((U)paramIcOrigenNoNullable.ElementAt(i));
}
}
But I have a problem because I can't convert U to T.
The idea was to create a generic method to convert for example long to long? in this case, but int to int? or any other non nullable basic type to a nullable type.
Is it possible or I should to create one method for each type?
This is motivated byt this post: Fastest way to convert List<int> to List<int?> that says it is better to use a foreach and don't use linq select or linq cast because it is slower.
Thanks.
PD: I give as answer to V0ldek, because it is what I was really asked in this post, but really it is faster if I use linq select, how PavelAnikhouski tells in some comment, at least using Entity Core 3.0. I don't know if in another versions is faster the for option, because the results in the link that I indicate, the times are very differnt than in the case of PavelAnikhouski and in my own case too.