leora
leora

Reputation: 196861

How to deal with duplicate anonymous type member conflicts?

I am creating an anonymous type and I have conflicting field names.

The following code is failing because i.Name and i.Target.Name both have a property with the same name; "Name".

How do I get around this ? Here is the code:

i => new
{
    i.Name,
    i.Target.Name,
    i.EndDate,
    i.LastUpdated
};

Upvotes: 11

Views: 2596

Answers (2)

bkaid
bkaid

Reputation: 52093

 i => new
      {
          i.Name,
          TargetName = i.Target.Name,
          i.EndDate,
          i.LastUpdated
        });

Upvotes: 5

Donnie
Donnie

Reputation: 46943

Name the anonymous fields, as such:

new {Name = i.Name, targetName = i.Target.Name, ... };

Upvotes: 31

Related Questions