views:

250

answers:

6

Hello

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!

+3  A: 
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.

Andrey
You can't return `IEnumerable<anonymous type>`
Greg
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?
Lorenzo
@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.
Andrey
@Greg i fixed answer
Andrey
@Andrey: thnaks for helping. Sorry for ignorance but was my first time using Anonymous types. These answers have been very useful to me!
Lorenzo
+11  A: 

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.

Greg
Option 5. Return a List of two-item Tuples (ex: `List<Tuple<int, string>>`). (but go for option 1.)
Anthony Pegram
Option 3 say that I miss Dinamic runtime binder. What is that? Should I import something?
Lorenzo
+1 Option 1 is really the best to reuse the type in different methods.
Jordão
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.
siride
@Greg: does Option 3 works even if the returned List<object> is inside a satellite DLL?
Lorenzo
@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.
Greg
+1  A: 

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.

Mike Webb
o is not of type Contact, it's an anonymous type.
Paul Ruane
this is wrong. o is not instance of `Contact`, but instance of anonymous type
Andrey
Oh, missed that part. Read it too fast. The others are correct. Use 'var'.
Mike Webb
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.
Greg
I guess I need to look more into anonymous types. Fixed yet again.
Mike Webb
@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`.
Greg
@Greg - You can if you define and use a Contact class/struct instead of an anonymous type as I have said above.
Mike Webb
@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.
Greg
@Greg - I wasn't being specific enough. I added the code to better explain what I was thinking.
Mike Webb
+1  A: 

replacing object with var in for each construct may work

saurabh
`List<var>` is what you suggest?
Andrey
+2  A: 

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.

Jordão
This is grotesque, yet fascinating.
Greg
A: 

i would use

allContacts
 .Select(c => new { c.ContactID, c.FullName })
 .ToList()
 .ForEach(c => {
   ...do stuff;
 });

then u dont need to declare at all. i believe that less declaration, less semi-colon leads to less bug

888
I think you've missed the real question. Please read it again carefully
Lorenzo