我想通过实体框架绑定gridview但它会抛出错误 像 -
匿名类型不能具有同名Entity Framwrok
的多个属性
这是我的方法。
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();
}
}
这里显示错误。 usr.UserId。
答案 0 :(得分:22)
直接问题是匿名类型new { m.UserId, u.UserId }
:两次同名。您可以通过提供显式属性名称来解决此问题,例如:new { u1 = m.UserId, u2 = u.UserId }
。
但接下来的问题是,定义连接的两个匿名类型都不会具有相同的属性名称,因此最终修复是这样的:
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();
}
}
答案 1 :(得分:6)
@Gert回答是正确的。只想显示更简单的解决方案 - 仅为第一个UserId属性命名:
on new { MembershipUserID = m.UserId, u.UserId }