Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An anonymous type cannot have multiple properties with the same name

I want to bind gridview through entity framework but it throws error like-

An anonymous type cannot have multiple properties with the same name Entity Framwrok

Here is my method.

public void UserList(GridView grdUserList)
{
    using (TreDbEntities context = new TreDbEntities())
    {

        var query =( from m in context.aspnet_Membership
                    from u in context.aspnet_Users
                    join usr in context.Users
                    on new { m.UserId, u.UserId } 
                    equals new { usr.MembershipUserID, usr.UserId }
                    into UserDetails
                    from usr in UserDetails
                    select new { 
                       CreationDate = m.CreateDate,
                       email = m.Email,
                       UserName = u.LoweredUserName,
                       Name = usr.FirstName + usr.LastNameLastName,
                       Active=usr.IsActive
                    }).ToList();
    }
}

It shows error here. usr.UserId.

like image 994
Mukesh Bhagat Avatar asked Sep 24 '13 09:09

Mukesh Bhagat


2 Answers

The direct issue is in the anonymous type new { m.UserId, u.UserId }: the same name twice. You can fix that by giving explicit property names, for example: new { u1 = m.UserId, u2 = u.UserId }.

But then the next issue will be that both anonymous types that define the join will not have the same property names, so the final fix is this:

public void UserList(GridView grdUserList)
{
    using (TreDbEntities context = new TreDbEntities())
    {
        var query =( from m in context.aspnet_Membership
                    from u in context.aspnet_Users
                    join usr in context.Users
                    on new { u1 = m.UserId, u2 = u.UserId } 
                    equals new { u1 = usr.MembershipUserID, u2 = usr.UserId }
                    into UserDetails
                    from usr in UserDetails
                    select new { CreationDate = m.CreateDate,
                                 email = m.Email,
                                 UserName = u.LoweredUserName,
                                 Name = usr.FirstName + " " + usr.LastName,
                                 Active = usr.IsActive
                               }
                   ).ToList();
    }
}
like image 95
Gert Arnold Avatar answered Oct 19 '22 02:10

Gert Arnold


@Gert answer is correct. Just want to show simpler solution - give name only to first UserId property:

on new { MembershipUserID = m.UserId, u.UserId } 
like image 33
Sergey Berezovskiy Avatar answered Oct 19 '22 03:10

Sergey Berezovskiy