gpt4 book ai didi

entity-framework - 匿名类型不能有多个同名属性

转载 作者:行者123 更新时间:2023-12-02 07:29:30 25 4
gpt4 key购买 nike

我想通过 Entity Framework 绑定(bind)gridview,但它抛出错误就像-

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。

最佳答案

直接问题在于匿名类型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();
}
}

关于entity-framework - 匿名类型不能有多个同名属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18978395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com