I ran into a problem this week regarding implicit conversions in C# on collections. While this (using implicit
) may not be our final approach, I wanted to at least finish out the code to offer the team as an option. I boiled down the problem to the following sample case:
I have two classes in my example: one that represents a business object (Foo) and one that represents the client version (View Object) of this business item (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 when I have a a List of Foo objects and want to convert them to a list of FooVO objects using my implicit operator.
List<Foo> foos = GetListOfBusinessFoos(); // Get business objects to convert
I tried
List<FooVO> fooVOs = foos; // ERROR
and
List<FooVO> fooVOs = (List<FooVO>) foos; // ERROR
and even
List<FooVO> fooVOs = foos.Select( x => x ); // ERROR
I know I can do this in a loop, but I was hoping for straightforward (LINQ?) way to convert the objects in one shot. Any ideas?
Thank you in advance.
Edit Fixed typo in example
FooVO
in that sample is actually returning aFoo
, is that intentional?