简体   繁体   中英

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.

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.

Bind only those columns needed to display the result. Turn off the AutoGeneratedColumns and add BoundField columns.

There are couple of issues with your query:

  1. Where part ( where c.id == a.userId ) does not make sense.
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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