views:

262

answers:

2

I'm trying to loop through the results of a function that is returning an anonymous object of results.

public static object getLogoNav()
{
  XDocument loaded = XDocument.Load(HttpContext.Current.Request.MapPath("~/App_Data/LOGO_NAV_LINKS.xml"));

  var query = from x in loaded.Elements().Elements()
              select new
              {
                 Name = x.FirstAttribute.Value,
                 Value = x.Value
              };

  return query;
}

codebehind page:

  var results = Common.getLogoNav();
  foreach(var nav in results) {
     string test = nav.Name;
  }
+3  A: 

You can't have an anonymous class as a return type in C# 3 (and 4 for that matter) and you can't cast an object to an anonymous type. Your three options are:

  • Doing the loop within the scope of the anonymous class (most of the time, this is the method)
  • Casting to object and using reflection (slow and not very easy to do unless you do some expression tree magic)
  • Converting to a named class and returning and instance of that.
  • (In C# 4) you can create some dynamic type magic to achieve a similar effect but that would be really the same as option 2 with some syntactic sugar.
DrJokepu
I have a hard time agreeing with that. Only because I have similar functions that are used as the datasource for some controls and those controls are able to properly loop through the results. That tells me that it's got to be somekind of casting needed to make this loop work. No?
phxis
"The name of an anonymous type is automatically generated by the compiler and cannot be referenced in program text." - Since you can't name it, you can't return it and can't cast to it. Except, of course, with Jon Skeet's the generic type parameter hack, but you really shouldn't use that. It's pure evil.
DrJokepu
phxis: You're right that you can return objects of anonymous type from a C# method. But those controls are essentially reflecting over those objects at runtime: they don't need compile-time access to the anonymous type. Whereas in your code you are trying to get compile-time access to the members, and you can't do that without the Horrible Hack Yuriy points to.
itowlson
DrJokepu: just to be clear, you *can* return an object of anonymous type. You just can't access its members by name at compile time. As phxis notes, this is often not an issue, for example when you plan to feed the object to a data bound control, which will be accessing the members through reflection anyway and doesn't need compile-time access to the type.
itowlson
@itowlson: Yes, of course you can return it. As an object. What I meant, you can't have the anonymous type as return type. C# is not as advanced as Fog Creek's Wasabi.
DrJokepu
I was returning it as an object hoping to get at those values in the results... However I ended up cutting out the function and just calling the query within the Page_Load and then looping through.
phxis
phxis: If i were you, I would create a named class for that and use that.
DrJokepu
+2  A: 

Jon Skeet wrote an entry about returning anonymous type. I hope you don't use it.

Yuriy Faktorovich