简体   繁体   English

我将如何编写在网格视图中显示用户名而不是用户ID的linq查询

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

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}; 

What I am looking is when uasing login in the webiste only to see problems they submitted. 我正在寻找的是在Webiste中登录时仅看到他们提交的问题。

Bind only those columns needed to display the result. 仅绑定显示结果所需的那些列。 Turn off the AutoGeneratedColumns and add BoundField columns. 关闭AutoGeneratedColumns并添加BoundField列。

There are couple of issues with your query: 您的查询有几个问题:

  1. Where part ( where c.id == a.userId ) does not make sense. 其中part( where c.id == a.userId )没有意义。
  2. Join with membership table - why is this needed? 加入会员表-为什么需要这样做? This join could potential give you multiple rows for one problem (if user has multiple roles) - eliminate if it's not needed. 这种连接可能会给您一个问题的多个行(如果用户具有多个角色)-如果不需要,则消除该行。

For example, 例如,

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
                       }; 

This will give you all problems along with user name - bind the grid column with "name" column. 这将给您带来所有问题以及用户名-将网格列与"name"列绑定。

If you want to list the problems for only logged in user then you have to add where part such as where u.user_id = currentUserId where currentUserId variable will hold the value of logged in user. 如果要列出仅登录用户的问题,则必须添加where部分,例如where u.user_id = currentUserId ,其中currentUserId变量将保存登录用户的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM