2
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?

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
fullyfish
  • 23
  • 4

3 Answers3

3
var resultList = moveList.SelectMany(m => sightList.Where( s => comparer
                                       .Equals((Vector3)s.position, m)).ToList();
cuongle
  • 74,024
  • 28
  • 151
  • 206
Henrik
  • 23,186
  • 6
  • 42
  • 92
1

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();
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

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();
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263