【问题标题】:Query Object's elements in c#在 C# 中查询对象的元素
【发布时间】:2011-11-09 06:37:52
【问题描述】:

当我尝试从 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>>>>
}

如何获取对象的值?

非常感谢!

【问题讨论】:

  • 你听过“一张图抵千言”这句话吗?这同样适用于代码。
  • 您能发布一些您遇到问题的代码吗?我不完全确定如何在没有上下文的情况下帮助构建答案。
  • Enrico, ci faresti vedere la LINQ query per piacere?
  • “元素”是什么类型的?您可能必须枚举 element.Itemselement.Controls 等。

标签: c# linq object iqueryable objectquery


【解决方案1】:

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

 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).
     }
 }

【讨论】:

  • Tejs,您的代码与应该返回对象的“property.GetValue(element,null)”分开,但对我来说不起作用......
  • 好的,我已经更正了 GetValue() 方法:object value= property.GetValue(item,null);现在我解决了我的问题!谢谢大家!
猜你喜欢
  • 2021-12-06
  • 2020-03-20
  • 1970-01-01
  • 2020-10-15
  • 2011-04-18
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多