Working with C# Anonymous Types

22,228

Solution 1

You can't return a list of an anonymous type, it will have to be a list of object. Thus you will lose the type information.

Option 1
Don't use an anonymous type. If you are trying to use an anonymous type in more than one method, then create a real class.

Option 2
Don't downcast your anonymous type to object. (must be in one method)

var list = allContacts
             .Select(c => new { c.ContactID, c.FullName })
             .ToList();

foreach (var o in list) {
    Console.WriteLine(o.ContactID);
}

Option 3
Use the dynamic keyword. (.NET 4 required)

foreach (dynamic o in list) {
    Console.WriteLine(o.ContactID);
}

Option 4
Use some dirty reflection.

Solution 2

foreach ( var o in list ) {
    Console.WriteLine( o.ContactID );
}

this will work only if list is IEnumerable<anonymous type>, like this:

var list = allContacts.Select(c => new {
        ContactID = c.ContactID,
        FullName = c.FullName
    });
}

but you can't return anonymous types, because you must define return type (you can't return var) and anonymous types can't have names. you should create non-anonymous type if you with to pass it. Actually anonymous types should not be used too much, except for inside of queries.

Solution 3

If you have a method like this:

  List<object> GetContactInfo() {
    List<object> list = new List<object>();
    foreach ( Contact c in allContacts ) { 
        list.Add( new { 
            ContactID = c.ContactID, 
            FullName = c.FullName 
        }); 
    } 
    return list;
  }

You shouldn't really do this, but there's a very ugly and not future-proof technique that you can use:

  static T CastByExample<T>(object target, T example) {
    return (T)target;
  } 

  // .....

  var example = new { ContactID = 0, FullName = "" };
  foreach (var o in GetContactInfo()) {
    var c = CastByExample(o, example);
    Console.WriteLine(c.ContactID);
  }

It relies on the fact (which can change!) that the compiler reuses anonymous types for types that have the same "shape" (properties names and types). Since your "example" matches the "shape" of the type in the method, the same type is reused.

Dynamic variables in C# 4 are the best way to solve this though.

Solution 4

You cannot do this with anonymous types. Just create a Contact class/struct and use that.

List<object> list = new List<object>();
foreach ( Contact c in allContacts ) {
    list.Add( c );
}

Then you can do this:

foreach ( var o in list ) {
    Console.WriteLine( o.ContactID );
}

...or this:

foreach ( object o in list ) {
    Console.WriteLine( ((Contact)o).ContactID ); //Gives intellisense
}

Of course you should in that case just do create a Contact list instead of an object list:

List<Contact> list = new List<Contact>();
foreach ( Contact c in allContacts ) {
    list.Add( c );
}

EDIT: Missed essential part of the question. Now fixed.
EDIT: Changed answer yet again. See above.

Share:
22,228

Related videos on Youtube

Lorenzo
Author by

Lorenzo

What should I say here?

Updated on October 29, 2020

Comments

  • Lorenzo
    Lorenzo over 3 years

    I am calling a method that returns a List variable that contains a c# Anonymous Type objects. For example:

    List<object> list = new List<object>();
    foreach ( Contact c in allContacts ) {
        list.Add( new {
            ContactID = c.ContactID,
            FullName = c.FullName
        });
    }
    return list;
    

    How do I reference this type properties in the code I am working on like for example

    foreach ( object o in list ) {
        Console.WriteLine( o.ContactID );
    }
    

    I know that my sample is not possible, I have only wrote that way to say that I need to identify each property of the anonymous type exactly.

    Thanks!

    Solution:

    Not just one of the answer is correct and/or suggest a working solution. I have ended up to using Option 3 of Greg answer. And I learned something very interesting regarding the dynamic in .NET 4.0!

  • Paul Ruane
    Paul Ruane over 13 years
    o is not of type Contact, it's an anonymous type.
  • Andrey
    Andrey over 13 years
    this is wrong. o is not instance of Contact, but instance of anonymous type
  • Andrey
    Andrey over 13 years
    List<var> is what you suggest?
  • Mike Webb
    Mike Webb over 13 years
    Oh, missed that part. Read it too fast. The others are correct. Use 'var'.
  • Greg
    Greg over 13 years
    You can't return IEnumerable<anonymous type>
  • Lorenzo
    Lorenzo over 13 years
    The return give me a warning that cannot do an implicit conversione between IEnumerable<Anonymous Type #1> and List<object>. How do I have to change my method signature?
  • Anthony Pegram
    Anthony Pegram over 13 years
    Option 5. Return a List of two-item Tuples (ex: List<Tuple<int, string>>). (but go for option 1.)
  • Lorenzo
    Lorenzo over 13 years
    Option 3 say that I miss Dinamic runtime binder. What is that? Should I import something?
  • Greg
    Greg over 13 years
    Won't work. list's type is List<object>. Thus var o is equivalent to Object o. Object does not have a property ContactID, therefore it would fail to compile.
  • Andrey
    Andrey over 13 years
    @Lorenzo as it was mentioned, you can't return anonymous types, because you must define return type and anonymous types can't have names. you should create non-anonymous type if you with to pass it. Actually anonymous types should not be used too much, except for inside of queries.
  • Greg
    Greg over 13 years
    This is grotesque, yet fascinating.
  • Jordão
    Jordão over 13 years
    +1 Option 1 is really the best to reuse the type in different methods.
  • Lorenzo
    Lorenzo over 13 years
    thnaks for helping. Sorry for ignorance but was my first time using Anonymous types. These answers have been very useful to me!
  • Mike Webb
    Mike Webb over 13 years
    I guess I need to look more into anonymous types. Fixed yet again.
  • Greg
    Greg over 13 years
    @Mike - It still doesn't work. The list doesn't contain instances of Contact, it contains instances of an anonymous type. You can't cast them to Contact.
  • Mike Webb
    Mike Webb over 13 years
    @Greg - You can if you define and use a Contact class/struct instead of an anonymous type as I have said above.
  • Greg
    Greg over 13 years
    @Mike - Yep, it would work to define another class. Contact is the name of the original class so a different name should be used for this new class.
  • Mike Webb
    Mike Webb over 13 years
    @Greg - I wasn't being specific enough. I added the code to better explain what I was thinking.
  • Lorenzo
    Lorenzo over 13 years
    I think you've missed the real question. Please read it again carefully
  • siride
    siride over 13 years
    Option 3 is unnecessary, really, when you can just use a custom class that is non-anonymous (nonymous?). It adds unnecessary overhead and doesn't buy you anything except fewer keystrokes.
  • Lorenzo
    Lorenzo over 13 years
    @Greg: does Option 3 works even if the returned List<object> is inside a satellite DLL?
  • Greg
    Greg over 13 years
    @Lorenzo, yes it would work regardless of where the list comes from. When you use the dynamic type, the call to o.ContactID will be resolved using the actual type of o at runtime. But be advised that dynamic is intended for interoperability with dynamic libraries, and - while it would work to use in your situation - you should really use option 1 if you can. Your code would be more maintainable.
  • jbtule
    jbtule over 13 years
    @Greg & @Lorenzo actually it would not work from a satellite dll unless it was a friend assembly. Anonymous types are internal, thus the dlr would throw a Runtime Binding Exception because it wouldn't be to bind due to permissions.