【问题标题】:Add my linq query resultset to a dataset, c# asp.net 3.5将我的 linq 查询结果集添加到数据集,c# asp.net 3.5
【发布时间】:2008-11-13 15:14:20
【问题描述】:

我的查询是:

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”添加到数据集或数据表中。谁能告诉我怎么做?

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

**注意:我使用的是 VS 2008 **

【问题讨论】:

    标签: c# asp.net linq data-binding


    【解决方案1】:
    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;
        }
    }
    

    没有 CopyToDataTable 除非您处理 DataRows 看看这些:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2162392&SiteID=1

    http://oakleafblog.blogspot.com/2007/03/linq-missing-todatatable-method-saga.html

    比我的代码涉及更多的好选择: http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx

    编辑: 更新后可以正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 2010-10-10
      • 2023-03-06
      • 1970-01-01
      • 2012-03-09
      相关资源
      最近更新 更多