匿名类型不能具有相同名称的多个属性 [英] An anonymous type cannot have multiple properties with the same name
问题描述
我想通过实体框架绑定gridview,但是会引发错误
like-
I want to bind gridview through entity framework but it throws error like-
匿名类型不能具有多个属性同名Entity Framwrok
An anonymous type cannot have multiple properties with the same name 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。
It shows error here. usr.UserId.
推荐答案
直接的问题是匿名类型 new {m.UserId,u .UserId}
:两次相同的名称。您可以通过提供明确的属性名称来解决此问题,例如: new {u1 = m.UserId,u2 = u.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();
}
}
这篇关于匿名类型不能具有相同名称的多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!