我应该如何将此源更改为LINQ?

时间:2013-04-25 08:36:39

标签: c# 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);
    }
}

我应该如何将此来源更改为linq?

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

这是more efficient,因为您需要一种join

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

答案 2 :(得分: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();