结合LINQ查询结果的GridView [英] binding linq query result to gridview

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

问题描述

我发现很难LINQ查询结果绑定到我的asp.net页面上的GridView控件。在主LINQ查询,我让所有的用户从满足来检查形式完成一栏的标准之一表 -

I am finding it difficult to bind the linq query results to a gridview control on my asp.net page. In the main Linq Query, i am getting all the users from one table which meets the criteria of a column that checks for "Form Completed" -

using (COLA_AccreditationEntities4 eFactory = new COLA_AccreditationEntities4())
{
    var evaluations = eFactory.Symposium_Evaluation.Where(a => 
            (!a.Completed.HasValue || 
              a.Completed.Value == 0) && a.Active && a.UserID >= 2063).ToList();

然后code里面我做了foreach循环来获取所有从该用户ID在previous表匹配其他表的用户ID -

Then inside the code i am doing a foreach loop to get all the userid from the other table that matches the userid in the previous table -

foreach (var eval in evaluations)
{
    var user = eFactory.SYMPOSIUM_Users.Where(a => a.UserID == eval.UserID.Value 
                         && a.Active.Value && a.UserRole == 1).FirstOrDefault();

    if (user != null && !users.Contains(user.UserID))
    {
        string name = user.FirstName + " " + user.LastName;
        string email = user.Email;
    }
}

我想要做的是在屏幕上显示的user.firstname + user.lastname + user.email。我用GridView控件绑定这一点,但没有得到预期的效果。

All i wanted to do is to show the user.firstname + user.lastname + user.email on the screen. I used gridview control to bind this, but not getting desired results.

这是如何实现这一点有什么想法?

Any thoughts on how to achieve this ?

推荐答案

您应该能够从您的查询直接绑定到您的网格:

you should be able to bind to your grid straight from your query:

`变种的用户= eFactory.SYMPOSIUM_Users.Where(A => a.UserID == eval.UserID.Value&放大器;&安培; a.Active.Value和放大器;&安培; a.UserRole == 1);
this.mygrid.DataSource =用户;

`var users = eFactory.SYMPOSIUM_Users.Where(a => a.UserID == eval.UserID.Value && a.Active.Value && a.UserRole == 1); this.mygrid.DataSource = users;

我会建议采取一看 asp.net网站网站的教程。很多很好的内容在那里。

I would suggest taking a look at the asp.net web site for tutorials. Lots of good content out there.

更新:结果
也许你的最好的办法就是在你的两个表创建一个加入一个匿名类型,这样您不必重复查询您的数据存储和数据绑定你会简单一些。

Update:
Perhaps your best solution is to create an anonymous type from a join on your two tables, this way your not repeatedly querying your data store, and your databinding will be simpler.

var result = (from i in eFactory.Symposium_Evaluation
              join user in eFactory.SYMPOSIUM_Users on i.UserID equals user.UserID
              where user.UserRole == 1 &&
                   ((!i.Completed.HasValue || 
                     i.Completed.Value == 0) && 
                    i.Active && a.UserID >= 2063)
              select new {
                  User = user,
                  Symposium = i
              }).ToList();

myDataGrid.DataSource = result;
myDataGrid.DataBind();

现在网格中的行模板,您可以:

Now in your grid row template you can:

<%#Eval("User.FirstName")%>
<%#Eval("User.LastName")%>
<%#Eval("User.Email")%>

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

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