[英]Using C# Linq to query nested objects
I have a PowerShell object and I want to filter the results. 我有一个PowerShell对象,我想过滤结果。
public Collection<PSObject> Groups;
I have successfully achieved this using linq if I need to filter based on a direct member. 如果需要基于直接成员进行过滤,则可以使用linq成功实现此目的。 But what if I want to filter based on an object 1 or 2 levels deep?
但是,如果我要基于1或2级深的对象进行过滤怎么办?
Here is the linq query to filter on a direct member: 这是要过滤直接成员的linq查询:
var groupMembershipFilter = (dynamic)CsQ.Groups.Where(x => x.Members["AgentsByUri"].Value != null).ToList();
However I need to drill down another level. 但是,我需要向下钻取另一个层次。 In PowerShell this would be easy:
在PowerShell中,这很容易:
$x.AgentsByUri.AbsoluteUri
I have tried all sorts but cant figure out how to make this work. 我已经尝试了各种方法,但无法弄清楚如何进行这项工作。 So that you can better understand the object structure here are a couple of screen shots:
为了更好地理解对象结构,这里有几个屏幕截图:
From the above screen shots you can see the "AgentsByUri" is a collection. 从上面的屏幕截图中,您可以看到“ AgentsByUri”是一个集合。 Inside that collection I want to test if the property "AbsoluteUri" contains a certain string value.
在该集合内,我想测试属性“ AbsoluteUri”是否包含某个字符串值。
The other thing I dont understand is why I have to use "Members" and cant just use "BaseObject" - this structure look far more similar to PowerShell and would be my preference if possible as it translates better to my PowerShell brain. 我不明白的另一件事是为什么我必须使用“成员”而不能仅使用“ BaseObject”-这种结构看起来与PowerShell更加相似,如果可能的话,我会优先选择它,因为它可以更好地转换为PowerShell的大脑。
Excuse my terminology, I'm reasonably new to C#! 不好意思,我是C#的新手! Hopefully this makes sense :)
希望这是有道理的:)
Any help or guidance would be much appreciated. 任何帮助或指导将不胜感激。
尝试这个:
var groupMembershipFilter = (dynamic)CsQ.Groups.Where(x => x.Members["AgentsByUri"].Any(x => x.AbsoluteUri == "url")).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.