[英]Iterate over a collection of anonymous type in .NET
I'm OK with both C# and VB.NET
我对C#和VB.NET都很好
I have a function GetListOfBook
, that returns LINQ to SQL result that has a collection of objects like the following: 我有一个函数
GetListOfBook
,它将LINQ返回到SQL结果,该结果具有如下对象的集合:
var result = GetListOfBook(); var result = GetListOfBook();
So inside the collecton, it looks like this: 因此,在collecton内部,它看起来像这样:
So inside the "result" collection it looks like this: 因此,在“结果”集合中,它看起来像这样:
{Book = {Book}, Category = "English", Author = {Author}}
{Book = {Book}, Category = "English", Author = {Author}}
{Book = {Book}, Category = "Web Development", Author = {Author}}
I want to iterate over each item in the collection to get the Book title and ISBN, Category and the author name. 我想遍历集合中的每个项目,以获取书名和ISBN,类别和作者名。 Something like this:
像这样:
foreach (var r in result)
{
Respone.Write(r.Book.Title, r.Book.ISBN, r.Category, r.Auhtor.Name);
}
At the moment, I cannot iterate over the collection yet. 目前,我还不能迭代该集合。 Thank you for any suggestion.
感谢您的任何建议。
Update: 更新:
Sorry for the trouble. 抱歉,添麻烦了。 This is actually working.
这实际上是有效的。 I found the typo in the code.
我在代码中发现了错字。
You still need to use the correct syntax for foreach
which requires you to specify a type for the loop variable. 您仍然需要为
foreach
使用正确的语法,这需要您为循环变量指定类型。 Since you can't name it explicitly, you need to use var
which infers the type. 由于无法明确命名,因此需要使用
var
来推断类型。
foreach (var r in result)
{
Respone.Write(r.Book.Title, r.Book.ISBN, r.Category, r.Auhtor.Name);
}
You need to use an implicitly typed variable using var
for the iteration variable within foreach
, just as you presumably have for your query to start with: 您需要使用一个隐式类型的变量,将
var
用作foreach
的迭代变量,就像您想让查询以以下内容开头一样:
var result = ...; // Your existing query
// r is implicitly typed here
foreach (var r in result)
{
Response.Write(r.Book.Title, r.Book.ISBN, r.Category, r.Author.Name);
}
EDIT: Looking more closely at your code, I suspect this is the problem: 编辑:仔细查看您的代码,我怀疑这是问题:
var result = GetListOfBook();
This can't be strongly typed, if it's returning an anonymous type... which means it must be returning something like IEnumerable
or IEnumerable<object>
. 如果返回匿名类型,则不能强类型化……这意味着它必须返回
IEnumerable
或IEnumerable<object>
。 I suspect you'll need to create an appropriate "normal" type to contain the results from GetListOfBook
- or perform the query in the same method that does the Response.Write
call. 我怀疑您需要创建一个适当的“普通”类型来包含
GetListOfBook
的结果-或以与Response.Write
调用相同的方法执行查询。
you can use reflection like this: 您可以像这样使用反射:
foreach (var r in result)
{
PropertyInfo info = r.GetProperty("Category");
Response.Write(info.GetValue(r, null));
}
You can use reflection to access the anonymous type's properties. 您可以使用反射来访问匿名类型的属性。
You can see some samples here: http://blogs.msdn.com/b/wriju/archive/2007/10/26/c-3-0-anonymous-type-and-net-reflection-hand-in-hand.aspx 您可以在此处看到一些示例: http : //blogs.msdn.com/b/wriju/archive/2007/10/26/c-3-0-anonymous-type-and-net-reflection-hand-in-hand .aspx
But you should do it really as a last resort if you MUST use anonymous types rather than explicit types. 但是,如果您必须使用匿名类型而不是显式类型,那么您应该真正将其作为最后的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.