使用LINQ查询嵌套动态对象 [英] Using Linq to query nested dynamic objects

查看:140
本文介绍了使用LINQ查询嵌套动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的LINQ查询访问成员的名称。以下成功返回集合中的所有成员名称值。

I have used the following linq query to access the member Names. The following successfully returns all member Name values within the collection.

var we = CsQ.Groups.SelectMany(g => g.Members).Where(a => a.Name == "Name").Select(b => b.Value).ToList();



我想现在所谓的绝对URI成员,这是嵌套在一个范围内筛选基于下来他人财产物业AgentsByUri。这并不工作,但给人的结构的一个想法:

I want to now filter this down based another property within "Members" called AbsoluteUri which is nested in a property AgentsByUri. This doesnt work but gives an idea of the structure:

var uri = CsQ.Groups.SelectMany(g => g.Members).Where(a => a.Name == "AgentsByUri").Select(b => b.Value).//??? I NEED TO NOW ACCESS "AbsoluteUri"



我怎样才能结合这些到一个查询,这样我就可以返回所有包含someValue中一个绝对URI中的名称。 。绝对URI似乎是嵌套在嵌套在AgentsByUri这更增加了复杂性集合多数民众赞成

How can I combine these in to one query so that I can return only the "Names" that have an "AbsoluteUri" that contains "SomeValue". AbsoluteUri seems to be nested in a collection thats nested in AgentsByUri which adds to the complication.

您可以看到AgentsByUri对象,这里的结构 - 的使用C#的LINQ查询嵌套对象

You can see the structure of the AgentsByUri object here - Using C# Linq to query nested objects

请原谅我的术语,我有理由新的C#!希望这是有道理的:)

Excuse my terminology, I'm reasonably new to C#! Hopefully this makes sense :)

任何帮助或指导非常赞赏:)

Any help or guidance VERY appreciate :)

EDIT3
获得的地方!铸造动态部分的工作 - member.AgentsByUri现在是好的,我无法弄清楚如何将其应用到查询的其余部分。尝试添加由没有运气的不同位置。

EDIT3 Getting somewhere! Casting as dynamic partially working - member.AgentsByUri is now OK, just cant figure out how to make it apply to the rest of the query. Tried adding in various locations by no luck.

< STRONG> EDIT2
感谢大家的投入。我还没有得到任何进一步的成功。我认为最大的问题是,我处理这是在运行时动态生成的PowerShell的对象。其结果是因为编译器没有任何关于他们我无法访问类/对象。为了解决这个问题我用的是动态类型,它可以让编译器相信什么,我将提供在运行时有效。我可以在LINQ查询转换为动态的? ?或者我需要去了解这个以不同的方式

EDIT2 Thanks for everyone's input. I haven't had any further success. I think the biggest problem is that I am dealing with a PowerShell object which is dynamically generated at run time. As a result I cannot access the classes/object because the compiler doesn't yet no about them. To get around this I use the "dynamic" type which allows the compiler to trust that what I provide will be valid at run time. Can I cast as dynamic in a linq query? Or do I need to go about this in a different way?

继承人我的例子得到给:

Heres what I get with the examples give:

试图访问动态对象时,编译器错误

EDIT1 (点击放大,图像高清晰度) :

EDIT1 (click and zoom, image is high res):

推荐答案

我只是写下来的基础上,图像您所提供。但是,我无法检查的正确性没有周围的代码,所以没有任何保证。

I'm just writing this down, based on the image you provided. However, I can not check for correctness without the surrounding code, so no guarantees.

var uri = CsQ.Groups
    .SelectMany(g => g.Members)
    .Where(m => m.AgentsByUri.SelectMany(a => a.Value, (a, v) => v.AbsoluteUri).Contains("SomeValue"))
    .Select(b => b.Value)

在LINQ语法应该在CsQ.Groups
更加清晰

In LINQ syntax it should be much clearer

var uri =
    from group in CsQ.Groups
    from dynamic member in group.Members
    where
        (from agent in (IEnumerable<dynamic>)member.AgentsByUri
         where agent.Name = "AgentsByUri"  // this line may be redundant
         from x in (IEnumerable<dynamic>)agent.Value
         select x.AbsoluteUri).Contains("SomeValue")
    select member.Value;



编辑:在我的第二个评论的建议不是做得比较工作。我通过明确铸造改变了代码在LINQ语法上面账户动态对象作为源的IEnumerable<动态> 。但是请注意,中投将一个值类型的枚举失败。
我希望这会为你工作。

The suggestion in my second comment does not quite work. I changed the code in LINQ syntax above to account for dynamic objects as the source by explicitly casting to IEnumerable<dynamic>. Note however that the cast will fail for an enumeration of a value type. I hope this will work for you.

这篇关于使用LINQ查询嵌套动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆