我有一个包含 3 个表的数据库:
User{UserId,UserName}
Role{RoleId,RoleName}
User_Role{UserId,RoleId}
这个查询:
int userIdPassByUrl = 0;
MyDbContext ctx = new MyDbContext();
var query = (from role in ctx.Role
join userRole in ctx.User_Role on role.RoleId equals userRole.RoleId
where userRole.UserId == userIdPassByUrl
select new { role.RoleId, role.RoleName }).Distinct();
我需要在带有 EntityDataSource 的 Gridview 中显示上述查询的结果,代码或在设计模式中设置它。
这是我的实体数据源:
<asp:EntityDataSource ID="EdsRolesByUser" runat="server"
ConnectionString="name=myDbEntities"
DefaultContainerName="myDbEntities" EnableFlattening="False"
EntitySetName="Roles" EntityTypeFilter="Role"
Select="it.[RoleId], it.[RoleName]">
</asp:EntityDataSource>
如有任何帮助,我们将不胜感激。
最佳答案
终于明白了。 必须修改 EntityDataSource 删除 EntitySetName 和 EntityTypeFilter 属性,并像这样添加 CommandText:
CommandText="SELECT DISTINCT userRole.RoleId, role.RoleName FROM Role AS role
INNER JOIN User_Role as userRole
ON role.RoleId = userRole.RoleId
WHERE userRole.UserId = @UserIdPassbyUrl"
这个链接帮助我: http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx
关于c# - EntityDataSource 查询内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10878713/