I have written code that works, but I can't seem to find a better way to combine the lists together if they have the same index.
class Apple {};
class Carrot {};
var apples = new list<Apple>();
var carrot = new list<Carrot>();
var combine = from a in apples
from c in carrots
where apples.IndexOf(a) == carrots.IndexOf(c)
select new {a, c};
(When I say combine, I don't mean append to the end of the list. {{a,b},{a,b}, .... { }}: Maybe I have the terminology wrong when trying to research.)
You can use Enumerable.Zip
:
var combine = apples.Zip(carrots, (a, c) => new { Apple = a, Carrot = c});
apples.Select((a,i)=> new { Apple = a, Carrot = carrots[i] });
That should work too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With