0

当我尝试从 c# 中的对象检索查询结果时遇到问题。

我做了一个返回对象元素的 linq 查询,我想在 c#(服务器端)中获取所有元素的值......

我不能这样做,我不知道为什么!

我试过了:

forech(var x in element)
{
  string titolo= x.title.ToString();
}

dynamic temp=(dynamic)element;

string titolo=temp.title.ToString();

和别的....

我可以看到对象类型是:

{
System.Data.Objects.ObjectQuery<<>f__AnonymousType26<int,string,string,bool?,int?,System.Linq.IQueryable<<>f__AnonymousType25<string>>>>
}

如何获取对象的值?

非常感谢!

4

1 回答 1

2

如果您正在寻找附加到元素的属性,您可以执行以下操作:

 foreach(var item in element)
 {
     foreach(var property in item.GetType().GetProperties())
     {
          // property.Name = Name of property.
          // property.GetValue(element, null) - Gets the value of the property (as System,Object).
     }
 }
于 2011-09-02T15:38:21.973 回答