简体   繁体   中英

Get the item from a collection that contains another collection using linq

Say I have a collection of Layer objects and each Layer may or may not contain a Polygon collection. There's only one Layer that contains the Polygon collection from the Layer collection. How do I obtain this Layer from the collection using LINQ?

Something like

foreach (var layer in Layers)
{
    var item = layer.FirstOrDefault( x =>x.Content is Polygon)
    if (item != null) return layer
}

您应该能够获得具有以下多边形内容的第一层:(使用Enumerable.Any ):

return Layers.FirstOrDefault(l => l.Any(x => x.Content is Polygon));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM