This week I ran into a problem regarding implicit conversions in C # in collections. Although this (using implicit ) may not be our final approach, I wanted to at least finish the code to offer the command as an option. I welded the problem to the following example:
I have two classes in my example: one that represents the business object (Foo) and one that represents the client version (View Object) of this business element (FooVO), as defined below ...
public class Foo { public string Id {get; set;} public string BusinessInfo {get; set;} } public class FooVO { public string Id {get; set;} public static implicit operator FooVO( Foo foo ) { return new FooVO { Id = foo.Id }; } }
My problem is that I have a list of Foo objects and you want to convert them to a list of FooVO objects using my implicit operator.
List<Foo> foos = GetListOfBusinessFoos();
I tried
List<FooVO> fooVOs = foos;
and
List<FooVO> fooVOs = (List<FooVO>) foos;
and even
List<FooVO> fooVOs = foos.Select( x => x );
I know I can do this in a loop, but I was hoping for a simple (LINQ?) Way to convert objects into a single snapshot. Any ideas?
Thanks in advance.
Edit Fixed typo in example
Setho source share