如何从List< dynamic>()获取数据?
我的代码是这样的
var eventDocs = new List<dynamic>();
foreach (var node in eventtypeNode.GetDescendantNodes())
{
string files = node.GetProperty("document").Value;
eventDocs.Add(new { Id = node.Id, Name = node.Name, CreatedOn = node.CreateDate, Path = files });
}
这很好。现在,我试图从此动态列表中获取数据
This works good. Now I am trying to fetch the data out of this dynamic list
foreach (var eventDoc in eventDocs)
{
eventDoc.---- //nothing comes on intellisence
}
没有来智能感知?我做错什么了吗?
Nothing comes on IntelliSense? Am I doing anything wrong?
您不会从Intellisense那里得到任何东西,因为您已经得到了列表<动态>
。您是在说:我不知道在编译时此列表将包含什么。当我访问元素的成员时,只需在执行时动态绑定它即可。
You won't get anything from Intellisense precisely because you've got a List<dynamic>
. You're saying, "I don't know at compile-time what this list will contain. When I access members of the elements, just bind that dynamically at execution-time."
鉴于您将绑定时间推迟到执行时间,为什么您会惊讶于Intellisense无法分辨列表中的内容?
Given that you're deferring binding to execution time, why would you be surprised that Intellisense can't tell what will be in the list?
对我来说,您应该更改代码以使用LINQ查询开头-然后您可以拥有一个具有已知元素类型的列表,该类型将是匿名类型。
It looks to me like you should change your code to use a LINQ query to start with - then you can have a list with a known element type, which will be an anonymous type.
var eventDocs = eventtypeNode.GetDescendantsNodes()
.Select(node => new { Id = node.Id,
Name = node.Name,
CreatedOn = node.CreateDate,
Path = node.GetProperty("document").Value })
.ToList();