2013-04-25 73 views
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); 
    } 
} 

我該如何改變這個源代碼到linq中?我應該如何將此源代碼轉換爲LINQ?

回答

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

我不知道你真的將其更改爲LINQ版本...

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(); 
1

這是more efficient既然你想一種join

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