我有一个包含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中显示上述查询的结果,无论是代码还是在设计模式下设置它。
这是我的EntitydataSource:
<asp:EntityDataSource ID="EdsRolesByUser" runat="server"
ConnectionString="name=myDbEntities"
DefaultContainerName="myDbEntities" EnableFlattening="False"
EntitySetName="Roles" EntityTypeFilter="Role"
Select="it.[RoleId], it.[RoleName]">
</asp:EntityDataSource>
任何帮助都将不胜感激,谢谢。
答案 0 :(得分:1)
终于明白了。 必须修改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