Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将我的linq查询结果集添加到数据集c#asp.net 3.5_C#_Asp.net_Linq_Data Binding - Fatal编程技术网

将我的linq查询结果集添加到数据集c#asp.net 3.5

将我的linq查询结果集添加到数据集c#asp.net 3.5,c#,asp.net,linq,data-binding,C#,Asp.net,Linq,Data Binding,我的问题是: var query1 = from u in dc.Usage_Computers.AsEnumerable where u.DomainUser == s3 orderby u.OperationTime descending select new { u.ProgramVersion, u.Operation

我的问题是:

var query1 = from u in dc.Usage_Computers.AsEnumerable
             where u.DomainUser == s3
             orderby u.OperationTime descending
             select new
             {
                 u.ProgramVersion,
                 u.OperationTime,
                 u.IPaddress,
                 u.ComputerName,
                 u.DomainUser,
                 u.OnNetwork,
                 Operation = u.Operation == 1 ? "Login" :
                             u.Operation == 2 ? "Logoff" :
                             u.Operation == 3 ? "AGNS Connect" :
                             u.Operation == 4 ? "AGNS Disconnect" :
                             "None"
             };

GridView1.DataSource = query1;
GridView1.DataBind();
使用gridview进行数据绑定后,我想将结果集“query1”添加到dataset或datatable中。有人能告诉我怎么做吗

我在这里看到了另一篇有同样问题的帖子,但那个答案在我的帖子里不起作用

**注意:我正在使用VS2008**

使用系统;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

public static class IEnumerableExt
{
    public static DataTable ToDataTable<T>(this IEnumerable<T> things) where T : class
    {
        DataTable tbl = new System.Data.DataTable();
        bool buildColumns = false;
        foreach (var item in things)
        {
            Type t = item.GetType();
            var properties = t.GetProperties();
            if (!buildColumns)
            {
                foreach (var prop in properties)
                {
                    Type ptype = prop.PropertyType;
                    if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        ptype = Nullable.GetUnderlyingType(prop.PropertyType.UnderlyingSystemType);
                    }
                    DataColumn col = new DataColumn(prop.Name, ptype);
                    tbl.Columns.Add(col);
                }
                buildColumns = true;
            }
            DataRow row = tbl.NewRow();

            foreach (var prop in properties)
            {
                if (prop.GetValue(item, null) == null)
                {
                    row[prop.Name] = DBNull.Value;
                }
                else
                {
                    row[prop.Name] = prop.GetValue(item, null);
                }
            }

            tbl.Rows.Add(row);
        }

        return tbl;
    }
}
使用System.Collections.Generic; 使用System.Linq; 使用系统数据; 公共静态类IEnumerableExt { 公共静态数据表ToDataTable(这是IEnumerable things),其中T:class { DataTable tbl=新的System.Data.DataTable(); boolbuildcolumns=false; foreach(事物中的var项) { 类型t=item.GetType(); var properties=t.GetProperties(); 如果(!buildColumns) { foreach(属性中的var属性) { 类型ptype=prop.PropertyType; if(prop.PropertyType.IsGenericType&&prop.PropertyType.GetGenericTypeDefinition()==typeof(可为空)) { ptype=Nullable.GetUnderlyingType(prop.PropertyType.UnderlyingSystemType); } DataColumn col=新的DataColumn(prop.Name,ptype); tbl.列。添加(列); } buildColumns=true; } DataRow row=tbl.NewRow(); foreach(属性中的var属性) { if(prop.GetValue(item,null)==null) { 行[prop.Name]=DBNull.Value; } 其他的 { 行[prop.Name]=prop.GetValue(项,空); } } tbl.行.添加(行); } 返回tbl; } }
除非处理数据行时注意以下几点,否则不存在CopyToDataTable:

好的替代方案比我的代码更复杂:

编辑: 进行了更新,使其工作正常