开发者

Feed a LINQ result into a DataROW

开发者 https://www.devze.com 2023-03-21 23:12 出处:网络
This works: var Result = from e in actual.Elements select new { Key = e.Key, ValueNumber = e.Value.ValueNumber,

This works:

var Result = from e in actual.Elements
                         select new
                         {
                             Key = e.Key,
                             ValueNumber = e.Value.ValueNumber,
                             ValueString = e.Value.ValueString,
                             ValueBinary = e.Value.ValueBinary,
                             ValueDateTim开发者_如何学编程e = e.Value.ValueDateTime
                         };

But this doesn't work:

IEnumerable<DataRow> Result = from e in actual.Elements
                select new DataRow
                {
                    Key = e.Key,
                    ValueNumber = e.Value.ValueNumber,
                    ValueString = e.Value.ValueString,
                    ValueBinary = e.Value.ValueBinary,
                    ValueDateTime = e.Value.ValueDateTime
                };

DataTable dt = Result.CopyToDataTable(Result);

Can you fix it for me? I want the second bit of code to work so that I can put it into the DataTable. I realize the syntax is totally wrong in #2. How do you specify a column using LINQ like this?


You can write a simple extension method that takes any IEnumerable<T>, uses reflection to get the PropertyDescriptors associated with T, and creates a DataColumn for each

public static DataTable PropertiesToDataTable(this IEnumerable<T> source)
{
      DataTable dt = new DataTable();

      var props = TypeDescriptor.GetProperties(typeof(T));

      foreach (PropertyDescriptor prop in props)
      {
          DataColumn dc = dt.Columns.Add(prop.Name,prop.PropertyType);
          dc.Caption = prop.DisplayName;
          dc.ReadOnly = prop.IsReadOnly;
      }

      foreach (T item in source)
      {
            DataRow dr = dt.Rows.NewRow();
            foreach (PropertyDescriptor prop in props)
                dr[prop.Name] = prop.GetValue(item);

            dt.Rows.Add(dr);
      }

      return dt;     
}


I can't offer much help other than to point you here:

http://blogs.msdn.com/b/aconrad/archive/2007/09/07/science-project.aspx


You might want to look into the DataTableExtensions.AsEnumerable Method

I haven't tested this, but this might get you pointed in the right direction:

IEnumerable<DataRow> result = (from e in actual.Elements
                              select new DataRow
                              {
                                  Key = e.Key,
                                  ValueNumber = e.Value.ValueNumber,
                                  ValueString = e.Value.ValueString,
                                  ValueBinary = e.Value.ValueBinary,
                                  ValueDateTime = e.Value.ValueDateTime
                              }).AsEnumerable();

DataTable dt = Result.CopyToDataTable(Result);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号