Typecast each object in an array in one line of code using LINQ
Let's say I have an array User
:
class User : IUserDowngraded
{
public Int32 Id { get; set; }
}
... with reduced functionality done by casting objects into a specific interface:
interface IUserDowngraded
{
Int32 Id { get; } // removes set functionality from property
}
... how would I transfer these downgraded ( IUserDowngraded
) objects User
to objects in one line of code using LINQ?
I am trying to avoid this:
// pseudo-code alert:
IUserDowngraded[] downgradedUsers { IUserDowngraded, IUserDowngraded, ... };
var upgradedUsers = new User[downgradedUsers.Count()];
Int32 i = 0;
foreach (IUserDowngraded du in downgradedUsers)
{
upgradedUsers[i] = (User)user;
i++;
}
0
a source to share
3 answers
Also, any of the implicit operators, Linq and Lambda expressions can make the code less readable. But which is more readable? methods should work.
0
a source to share