fullyfish
fullyfish

Reputation: 33

How should I change this source into LINQ?

List<Node> resultList = new List<Node>();
NodeEqualityComparer comparer = new NodeEqualityComparer();

foreach (Vector3 move in moveList)
{
    foreach (Node sight in sightList)
    {
        if (comparer.Equals((Vector3)sight.position, move))
            resultList.Add(sight);
    }
}

How should I change this source into linq?

Upvotes: 2

Views: 104

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460158

This is more efficient since you want a kind of join:

List<Node> resultList = moveList
    .Join(sightList, m => m, s => (Vector3)s.position, (m, s) => s, comparer)
    .ToList();

Upvotes: 1

Henrik
Henrik

Reputation: 23324

var resultList = moveList.SelectMany(m => sightList.Where( s => comparer
                                       .Equals((Vector3)s.position, m)).ToList();

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

I'm not sure you really have to change it to LINQ version ...

List<Node> resultList;
NodeEqualityComparer comparer = new NodeEqualityComparer();

resultList = (from m in moveList
             from s in sightList
             where comparer.Equals((Vector3)s.position, m)
             select s).ToList();

Upvotes: 0

Related Questions