An anonymous type cannot have multiple properties with the same name

entity-framework

Question

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.

1
18
9/26/2013 7:46:08 AM

Popular Answer

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();
    }
}
26
9/24/2013 2:03:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow