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.
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();
}
}
@Gert answer is correct. Just want to show simpler solution - give name only to first UserId property:
on new { MembershipUserID = m.UserId, u.UserId }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With