2012-06-04 36 views
2

我有一個DB 3個表:EntityDataSource查詢內部聯接

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> 

任何幫助,將不勝感激,謝謝。

+1

http://msdn.microsoft.com/en-我們/庫/ cc488524.aspx –

+0

嗨拉斐爾鏈接中的示例只有選擇和wheres只有一個表,我真正的問題是完成加入和我的查詢不同的操作。 – ramo2712

+0

所以它與entityDataSource無關,你只是要求一個工作linq2entity查詢?然後說出你真正的問題是什麼,以及你想從這個查詢中得到什麼... –

回答

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