views:

62

answers:

3

i have:

 IEnumerable<Foo> foolist

and i want to convert this to:

IEnumerable<Bar> barlist

is there a linq / lambda solution to move from one to the other

both objects (foo and bar) have simple properties that i am going to convert. for example:

 bar.MyYear = foo.Year

they each have about 6 properties

+4  A: 

You can do:

IEnumerable<Bar> barlist = foolist.Select(
         foo => new Bar(foo.Year)); // Add other construction requirements here...

Enumerable.Select is really a projection function, so it is perfect for type conversions. From the help:

Projects each element of a sequence into a new form.


Edit:

Since Bar doesn't have a constructor (from your comments), you can use object initializers instead:

IEnumerable<Bar> barlist = foolist.Select(
     foo => new Bar() 
                {
                    Year = foo.Year, 
                    Month = foo.Month
                    // Add all other properties needed here...
                });
Reed Copsey
@Reed Copsey - i dont follow why you are passing foo.Year into Bar(). bar has no constructor but rather a bunch of properties
ooo
@ooo: I edited my answer to show you how to make this work without a constructor.
Reed Copsey
Curious - why the downvote anybody - this seems like a perfectly reasonable answer...?
Reed Copsey
@Reed: -1 because you can't read minds.
ChaosPandion
@ChaosPandion: Ahh, okay - well deserved -1 then! Thanks for explaining. ;)
Reed Copsey
@Reed Copsey - thanks . . works like a charm . .
ooo
A: 

In terms of general data processing patterns, the map of the general Map/Reduce paradigm is the underlying principle we're looking for (which maps, if you'll pardon the pun, to Select in LINQ, as @Reed Copsey gives a good example of)

AutoMapper is a library which implements such convention based projection/mapping in environments where LINQ would apply and may be worth investigating for you if you have a sufficiently complex set of requirements to warrant introducing the cognitive overhead of bring another library to the party in your context.

Ruben Bartelink
Except that there's no reduce here - it's pure "map".
Reed Copsey
@Reed Copsey: Yes, v1 needed a lot of work. I was scrabbling for 'projection' but you've got that now so I wont nick it!
Ruben Bartelink
@Reed Copsey: Do you feel it makes more sense now? Some cowardly genius out there seems not to and wants to make it -1. I'd love to see their fantastic countering view represented as a glorious post here :D
Ruben Bartelink
I do wonder why I waste my time on this childish site sometimes.
Ruben Bartelink
Hehehe, well, I voted it up for you to counteract the -1, especially now that it's reworded. ;)
Reed Copsey
@Reed Copsey: Thanks! I'm amused that there are still two geniuses out there who disagree (and would probably deem your pity vote as encouraging bad behavior!) but dont want to explain their downvotes or offer alternatives. I feel a peer pressure badge on the way. I'd be better off trying out Farmville as a productive use of my time.
Ruben Bartelink
+1  A: 

If there is a reference (or identity) conversion between Foo and Bar, then this is an example of covariance or contravariance in generics.

C#4.0 supports covariance and contravariance in generics.

The code should look something like:

IEnumerable<Foo> foolist = new List<Foo>();

IEnumerable<Bar> barlist = foolist;

If you are < C#4.0 then Reed's answer is your baby.

More on this can be found at:

http://msdn.microsoft.com/library/dd799517(VS.100).aspx

jnielsen
+1 good point. From voting patterns (and the comment on the accepted answer) though, the consensus appears to be that he just wanted to copy the fields
Ruben Bartelink