我该如何将此源更改为LINQ
本文关键字:LINQ | 更新日期: 2023-09-27 18:11:01
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?
var resultList = moveList.SelectMany(m => sightList.Where( s => comparer
.Equals((Vector3)s.position, m)).ToList();
这是更有效的,因为您想要一种join
:
List<Node> resultList = moveList
.Join(sightList, m => m, s => (Vector3)s.position, (m, s) => s, comparer)
.ToList();
我不确定你是否真的必须将其更改为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();