I’m doing this.
List<Type1> t1 = ...
List<Type2> t2 = ...
List<Type3> t3 = new List<Type3>();
for(int i = 0; i < t1.Count; i++)
t3.AddRange(new Type3(t1[i], t2[i]));
I’ve tried to use foreach by deploying this.
IEnumerable<Type1> t1 = ...
IEnumerable<Type2> t2 = ...
IEnumerable<Object> t12 = t1.Zip(t2, (outer, inner)
=> new{ Outer = outer, Inner = inner});
List<Type3> t3 = new List<Type3>();
foreach(Object element in t12)
t3.Add(new Type3(element.Outer, element.Inner));
This doesn’t fly because Object doesn’t recognize the Outer and Inner properties. I can’t as it either, because it’s a anonymous type. What can I do?
I’d also prefer not to use Object or var but a Tuple or something that says “it’s a <Type1,Type2> combo kind of thing”.
NB. There’s this discussion but that doesn’t really answer my question. And in this one I just can’t get the fields.
You should be able to use the anonymous type correctly by using
varinstead forobjectin the loop.varinfers the type, and allows you to use the anonymous object as expected.If you would like to use
Tupleyou could do something like this: