0

I have a problem when i try to retrieve query results from object in c#.

I did a linq query that return object element and the i wanna get all elements value in c# (server side)...

I can't do this and i don't know why!

I tried:

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

and

dynamic temp=(dynamic)element;

string titolo=temp.title.ToString();

AND OTHERS....

I can see that the object type is:

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

How can i get object's values?

Thanks a lot!

Cédric Guillemette
  • 2,394
  • 1
  • 14
  • 22
Enricosoft
  • 869
  • 2
  • 10
  • 28

1 Answers1

2

If you're looking for the properties attached to the element, you could do something like this:

 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
  • 40,736
  • 10
  • 68
  • 86
  • Tejs, your code works apart the "property.GetValue(element,null)" that should be return an object, but for me doesn't work... – Enricosoft Sep 02 '11 at 17:11
  • Ok, i've corrected the GetValue() method that it will be: object value= property.GetValue(item,null); NOW I SOLVED MY PROBLEM! THANKS TO ALL! – Enricosoft Sep 02 '11 at 17:12