如何从List& lt; dynamic& gt()获取数据? [英] How to fetch data from 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
}
IntelliSense没事吗?我做错什么了吗?
Nothing comes on IntelliSense? Am I doing anything wrong?
推荐答案
您不会从Intellisense那里得到任何东西,因为您有 List< dynamic>
.您说的是:我不知道在编译时此列表将包含什么.当我访问元素的成员时,只需在执行时动态绑定即可."
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();
这篇关于如何从List& lt; dynamic& gt()获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!