C# GridView可以用var填充吗?

C# GridView可以用var填充吗?,c#,asp.net,sql-server,linq,C#,Asp.net,Sql Server,Linq,我试图在GridView中显示excel工作表和sql server数据表之间的重复行 这是我的代码: var matched = (from table1 in dt4.AsEnumerable() join table2 in oltlb.AsEnumerable() on table1.Field<int>("ID") equals table2.Field<int>("ID")

我试图在GridView中显示excel工作表和sql server数据表之间的重复行

这是我的代码:

var matched = (from table1 in dt4.AsEnumerable()
                join table2 in oltlb.AsEnumerable() on
                table1.Field<int>("ID") equals table2.Field<int>("ID")
                where table1.Field<string>("Data") == table2.Field<string>("Data")
                select table1);
DataTable dthi5 = new DataTable();
dthi5 = matched.CopyToDataTable();
var matched=(来自dt4.AsEnumerable()中的表1)
在上的oltlb.AsEnumerable()中联接表2
表1.字段(“ID”)等于表2.字段(“ID”)
其中table1.字段(“数据”)==table2.字段(“数据”)
选择表1);
DataTable dthi5=新DataTable();
dthi5=匹配的.CopyToDataTable();

我可以在GridView中显示var的值吗?

可以,它可以用变量填充。(在这种情况下,您必须将其强制转换为
.ToList()

您必须了解
var
是强类型的!这与php不同,php中可以切换变量的类型

试着写

var x = null;
在VisualStudio中。将会有一个错误

您的
var
每次都必须声明为类型安全。这只是一个短暂的过程,使程序员不必总是在变量前面写长的类型名。因此,在您的案例中,您的
var
是an
IEnumerable

试试这个

var matched = (from table1 in dt4.AsEnumerable()
                  join table2 in oltlb.AsEnumerable() on
                table1.Field<int>("ID") equals table2.Field<int>("ID")
                  where table1.Field<string>("Data") == table2.Field<string>("Data")
                  select table1);
      DataTable dthi5 = new DataTable();
      dthi5 = matched.CopyToDataTable();

myGridView.DataSource = dthi5;
myGridView.DataBind();

您完全可以将var绑定到gridview

假设有一个包含三个字段的employee表

emp_id emp_fname emp_lname

第一种方法:如果您将整个表或特定记录交给var,那么列表将起作用

一旦成功建立到sql server的连接,employees就是一个数据上下文类

var result = from alias in dc.employees
          where alias.emp_id == id --(this is the passed parameter)
您还可以手动指定id,例如:

where alias.emp_id == 5
          select alias;
将选择id=5的整个员工记录

您现在可以简单地将其绑定到gridview

gridview1.datasource = result.tolist();
gridview1.databind();
如果你把整张桌子都给我,那么托利斯特也行

var result  = from alias in dc.employess
              select alias;

gridview1.datasource = result.tolist();
gridview1.databind();
如果选择多个列,则tolist将不起作用。您需要返回对象

该方法已在class1中定义

public static object returnquery()
{

dcdatacontext dc = new dcdatacontext();

var result = from alias in dc.employees
             where alias.emp_id == 5
             select new
             {
                alias.emp_fname,
                alais.emp_lname
             };

 return result;
}
你需要抓住物体

object obj = new class1.returnquery();

gridview1.datasource = obj;
gridview1.databind();
或者你可以试试这个,看看它是否有效。实际上我还没试过

var result = from alias in dc.employees
             where alias.emp_id == 5
             select new
             {
                alias.emp_fname,
                alias.emp_lname
             };

gridview1.datasource= result.object();
gridview1.databind();

尝试将其转换为.ToList();请指定我需要添加的位置。ToList()?我是一名初学者。请尝试将
dth15
绑定到GridView。您的示例出现了严重错误!对不起,我是一个完全的新手,你能解释一下我应该在上面的代码中添加或替换到哪里吗?只需添加.ToList()-…选择table1.ToList();System.InvalidCastException:指定的强制转换无效。我又加了一句;正如您所说,然后是GridView1.DataSource=matched;
var result = from alias in dc.employees
             where alias.emp_id == 5
             select new
             {
                alias.emp_fname,
                alias.emp_lname
             };

gridview1.datasource= result.object();
gridview1.databind();