Working with interfaces, we commonly have a var or an IQueryable that is going to return a set of data objects that we will then cast to the interface and return as a List or IList, like this:
var items =
from t in SomeTable
where t.condition == true
select;
return items.ToList( ).Cast<SomeInterface>( ).ToList( );
NOTE: items.Cast( ).ToList( ) will compile, but will throw an InvalidCastException at run time.
Is there a better way? (I put the ToList/Cast/ToList in an extension method, but that's not really any better...)
return items.CastToList<SomeClass, SomeInterface>( );
Thanks!