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?

4

3 回答 3

3
var resultList = moveList.SelectMany(m => sightList.Where( s => comparer
                                       .Equals((Vector3)s.position, m)).ToList();
于 2013-04-25T08:42:46.557 回答
1

更有效,因为您想要一种join

List<Node> resultList = moveList
    .Join(sightList, m => m, s => (Vector3)s.position, (m, s) => s, comparer)
    .ToList();
于 2013-04-25T08:57:13.350 回答
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();
于 2013-04-25T08:40:27.393 回答