Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys. I'm new to using LINQ and I have a quick question. Basically I have an aspx page with a gridview on it. I'm binding values to the grid view from a return stored procedure. All this works fine, the problem I have is trying to use linq to make a join and display the results of that join.

The data the stored procedure returns contains a GroupID but im trying to join that ID to a Group table on the ID to retrieve the name of that group which will be bound to a label on the grid view. So instead of displaying "1" for example on grid view it will show "User". I have provided code of what I thought might work but it doesn't. Any advice where I'm going wrong. Hope I've explained this clear enough.

C#
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblGroup = (Label)e.Row.FindControl("lblGroupID");

                GroupBC groupBC = new GroupBC();
                UserBC userBC = new UserBC();

                List<Entities.UserInfo> users = userBC.ReturnUsers();
                List<Entities.GroupInfo> groups = groupBC.ReturnGroups();

                var groupQuery = from user in users
                                 join g in groups on user.GroupID equals g.GroupID
                                 select new { GroupID = g.Description };

                lblGroup.Text = groupQuery.ToString();
            }



Thanks in advance.
Posted
Comments
Ganesh KP 20-Sep-13 8:39am    
When u put a breakpoint what value are you getting to the groupQuery? If you are not getting any value, try to run sql statement first against the database and see whether you are getting any results.
RhysBush 20-Sep-13 12:12pm    
The groupquery does return values yeah. But trying to bind the data to the label seems the biggest problem.

var groupQuery = from user in users
join g in groups on user.GroupID equals g.GroupID
select new { GroupID = g.Description };


foreach(var v1 in var groupQuery)
{
lblGroup.Text = v1.GroupID.ToString();
}
 
Share this answer
 
Comments
RhysBush 20-Sep-13 11:59am    
I tried this but even if the group ID of the user is equal to 2, it still seems to show Group 1 in the grid view when it should show group 2. Any hints why?
C#
var groupQuery = (from user in users
                     join g in groups on user.GroupID equals g.GroupID
                     select g.Description).FirstOrDefault();

if(null != groupQuery)
{
  lblGroup.Text = groupQuery.ToString();
}
 
Share this answer
 
Comments
RhysBush 20-Sep-13 11:59am    
I tried this but even if the group ID of the user is equal to 2, it still seems to show Group 1 in the grid view when it should show group 2. Any hints why?
CodeBlack 23-Sep-13 5:02am    
In the above query put where condition for the group id

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900