views:

40

answers:

1

I have 2 Lists:

List<User>
List<UserStats>

So I want to add a property Count to User (it doesn't have one now, and I can't change the implementation at this point).

For a web service call, that returns json, I want to modify the User object.

Basically I add the Users to a collection. So I want to add a modified user class (via anonymous functions?) to the collection before I serialize it to json.

So something like:

loop users {
   user.Count = userstats[user.ID].Count;

   list.Add(user);
}

is this possible? how?

+3  A: 

Yes, you can use anonymous types for this:

users.ConvertAll(u => new {
    u.NormalProperty, // repeat for properties you want
    Count = userstats[user.ID].Count
});

You can probably achieve the same with a LINQ join, selecting out the anonymous type.

David M
so users = users.ConvertAll(...) do I have to cast to type?
Blankman
No, you can't set this back to the users variable. Declare a new one as `var extendedUsers = ` or similar.
David M