帮助LINQ结果和标签 [英] Help with LINQ results and Labels

查看:64
本文介绍了帮助LINQ结果和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我是使用LINQ的新手,我有一个简单的问题。基本上我有一个带有gridview的aspx页面。我正在从返回存储过程将值绑定到网格视图。所有这一切都很好,我遇到的问题是尝试使用linq进行连接并显示该连接的结果。



存储过程返回的数据包含一个GroupID但我试图将该ID加入ID上的Group表,以检索将绑定到网格视图上的标签的该组的名称。因此,不是在网格视图上显示1,而是显示用户。我提供了我认为可行的代码,但事实并非如此。我出错的任何建议。希望我已经足够清楚地解释了这一点。



  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 = 来自用户 用户
join g 组中的user.GroupID等于g。 GroupID
选择 new {GroupID = g.Description};

lblGroup.Text = groupQuery.ToString();
}







提前致谢。

解决方案

var groupQuery =来自用户的用户

在user.GroupID上加入g组等于g.GroupID

选择新的{GroupID = g.Description };





foreach(var group1中的var v1)

{

lblGroup.Text = v1.GroupID.ToString();

}


  var  groupQuery =(来自用户 用户
join g >等于g.GroupID
选择 g.Description).FirstOrDefault();

if null != groupQuery)
{
lblGroup.Text = groupQuery.ToString();
}


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.

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.

解决方案

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


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


这篇关于帮助LINQ结果和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆