EntityDataSource查询内部联接(EntityDataSource query inne

2019-09-17 17:52发布

我有3代表一个DB:

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();

我需要显示在GridView与EntityDataSource上述查询的结果,代码或在设计模式中设定它。

这是我的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>

任何帮助,将不胜感激,谢谢。

Answer 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



文章来源: EntityDataSource query inner join