How to convert this cycle into Linq.js or just linq

139 Views Asked by At

My code is written in JavaScript

for (var i = 0; i < tsUA.length; i++) {
        if (tsRU[i][0] === tsUA[i][0])
            tsUA[i][2] = tsRU[i][1];
        }

preferably using firstOrDefault

1

There are 1 best solutions below

1
Jeff Mercado On BEST ANSWER

In C# LINQ, you could do this:

var query = tsUA.Zip(tsRU, (ua, ru) => new { ua, ru })
    .Where(p => p.ua[0] == p.ru[0]);
foreach (var pair in query)
    pair.ua[2] = pair.rs[1];

Note, there isn't that much "LINQ" in here, you're modifying objects. LINQ is not designed to do that. And your request to use FirstOrDefault() makes no sense, it has no place here.

As you should probably notice, you're not gaining much by using LINQ here. Also, keeping your objects as arrays makes it less readable. Why not create proper objects to represent your data?