[英]How to cast object within a C# System.Linq lambda expression?
List<System.Guid> meshGuidList= new List<System.Guid>();
foreach (TreeGridItem meshcol in TerrainData.MeshCollection)
{
Globals.MeshProperty meshprop = meshcol.Tag as Globals.MeshProperty;
meshGuidList.Add(meshprop.MeshGuid);
}
I have a collection, in order to access it, I need to cast the items as TreeGridItem.我有一个集合,为了访问它,我需要将项目转换为 TreeGridItem。
After casting the items, I need to cast meshcol.Tag again as Globals.MeshProperty so i can access it.投射项目后,我需要将 meshcol.Tag 再次投射为 Globals.MeshProperty 以便我可以访问它。 Basically I want a list of meshprop.MeshGuid using Simple.Select function.基本上我想要一个使用 Simple.Select function 的 meshprop.MeshGuid 列表。
But the multiple casting feels a bit difficult.但是多重铸造感觉有点困难。
It is unclear what the Type
is that is returned from the MeshCollection
enumerator, but this syntax may work for you if all of the items in the collection can be cast to TreeGridItem
目前尚不清楚从MeshCollection
枚举器返回的Type
是什么,但如果集合中的所有项目都可以转换为TreeGridItem
,则此语法可能对您有用
List<System.Guid> meshGuidList = TerrainData.MeshCollection
.Cast<TreeGridItem>()
.Select(mesh => (mesh.Tag as Globals.MeshProperty).MeshGuid)
.ToList();
_You could split that Select
clause out to multiple lines if you wanted, but I'm not sure we gain much in readability or performance: _如果需要,您可以将Select
子句拆分为多行,但我不确定我们在可读性或性能方面获得了多少:
List<System.Guid> meshGuidList = TerrainData.MeshCollection
.Cast<TreeGridItem>()
.Select(mesh => mesh.Tag)
.Cast<Globals.MeshProperty>()
.Select(prop => prop.MeshGuid)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.