Reputation: 13763
class A
{
public string[] X {get;set;}
public string[] Y {get;set;}
}
class B
{
public string X {get;set;}
public string Y {get;set;}
}
transfer data of A's object to B's array with Linq? suppose Object of A have 10-10 size X and Y and I want to transfer into B array (B[] b = new B[10])
A a = new A();
//put 10 items in both x and y
B[] b = new B[10];
//here I want to get a's data to b
Upvotes: 2
Views: 235
Reputation: 75326
You can use Zip
method from LINQ:
A a = new A();
B[] bs = a.X.Zip(a.Y, (x, y) => new B() { X = x, Y = y })
.ToArray();
Or use Select
with index:
B[] bs = a.X.Select((x, i) => new B {X = x, Y = a.Y[i]})
.ToArray();
Another way using Enumerable.Range
if you get stuck on .NET 3.5:
B[] bs = Enumerable.Range(0, 10)
.Select(i => new B {X = a.X[i], Y = a.Y[i]})
.ToArray();
Upvotes: 5