Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ How to combine second item in Tuple after query using where on first item

Tags:

c#

linq

 Tuple<int, double>[] obs1;
 Tuple<int, double>[] obs2;

What I'd like to do is create Tuple<int, double>[] obs3

which contains those Tuple<int,double> which have the same int in both obs1 and obs2 and then set the corresponding double as:

obs3[i].Item2 =  obs1[i].Item2 + obs2[i].Item2

I tried finding the relevant Tuples this way:

var obs3 = from o in obs1 where obs2.Contains(o) select o;

But then I don't know how to access and use the Item2 double, instead I just get a subset of obs1.

I keep getting downmarked for my questions.I'm hoping its because I've not tried presenting a first try at a solution.

like image 274
gwizardry Avatar asked Jan 03 '23 18:01

gwizardry


1 Answers

You can join both arrays on Item1

var query = from t1 in obs1
            join t2 in obs2 on t1.Item1 equals t2.Item1
            select Tuple.Create(t1.Item1, t1.Item2 + t2.Item2);

Tuple<int, double>[] obs3 = query.ToArray();
like image 121
Tim Schmelter Avatar answered Jan 13 '23 19:01

Tim Schmelter