簡體   English   中英

我將如何編寫在網格視圖中顯示用戶名而不是用戶ID的linq查詢

[英]how would I write linq query that would display user name instead of userID in a gridview

我將如何編寫在網格視圖中顯示用戶名而不是用戶ID的linq查詢。

var q = from u in entities.problems
                       join a in entities.my_aspnet_membership on u.user_id equals a.userId
                       join c in entities.my_aspnet_users on a.userId equals c.id
                       where c.id == a.userId
                       select new { u.problem_id, c.name, u.problem_description, u.problem_reported_datetime, u.problem_history}; 

我正在尋找的是在Webiste中登錄時僅看到他們提交的問題。

僅綁定顯示結果所需的那些列。 關閉AutoGeneratedColumns並添加BoundField列。

您的查詢有幾個問題:

  1. 其中part( where c.id == a.userId )沒有意義。
  2. 加入會員表-為什么需要這樣做? 這種連接可能會給您一個問題的多個行(如果用戶具有多個角色)-如果不需要,則消除該行。

例如,

var q = from u in entities.problems
                       join c in entities.my_aspnet_users on u.user_id equals c.id
                       select new { 
                              u.problem_id, c.name, u.problem_description, 
                              u.problem_reported_datetime, u.problem_history
                       }; 

這將給您帶來所有問題以及用戶名-將網格列與"name"列綁定。

如果要列出僅登錄用戶的問題,則必須添加where部分,例如where u.user_id = currentUserId ,其中currentUserId變量將保存登錄用戶的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM