如何将一个通过LINQ查找出来并重新构造的匿名对象转化成DATATABLE

xiaogua 2008-04-16 02:39:23
如题:
DataTable resulttable=null;
IQueryable<DataRow> result=from table in tabledata
where table.field1='0215'
select new {字段1=table.field2+"-"+table.field4,字段2=table.field3};
resulttable = result.CopyToDataTable<DataRow>();

如上代码,但是转化出错,要如何转化,高手请指教
...全文
764 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinxinlong 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sp1234 的回复:]
哦,其实对象的只读属性也当然应该可以转化到DataRow里边的。

删除那两行 isReadOnly 判断!
[/Quote]
如果array中存在空值怎么办?
ReyZhang 2010-12-15
  • 打赏
  • 举报
回复
good,收藏~~~
fengart 2008-08-06
  • 打赏
  • 举报
回复
经过测试,发现sp1234的方法去掉"if (!dp.IsReadOnly) "语句即可。
下面贴出测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace Test
{
public class People
{
public string Name{get ;set;}
public string City { get; set; }
public string Sex { get; set; }
}

public static class Extension
{
public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
ret.Columns.Add(dp.Name, dp.PropertyType);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
// if (!dp.IsReadOnly)
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}

}


public class Linq2DataTable
{
private static List<People> GetData()
{
List<People> list = new List<People>();
list.Add(new People {Name="Bruce",City ="SZ",Sex ="Man"});
list.Add(new People { Name = "John", City = "GZ", Sex = "Man" });
list.Add(new People { Name = "Judy", City = "CS", Sex = "Woman" });
return list;
}

public static void Test()
{
List<People> data= GetData();
var result = from p in data where p.Sex == "Man" select new { Name = p.Name, City = p.City };
DataTable dt = Extension.CopyToDataTable(result);
Console.WriteLine(ToString(dt));
}

public static string ToString(System.Data.DataTable data)
{
StringBuilder sb = new StringBuilder();
int colCount = data.Columns.Count;
foreach (System.Data.DataRow dr in data.Rows)
{
for (int i = 0; i < colCount; i++)
{
string s = (dr[i] == null ? string.Empty : dr[i].ToString());
sb.Append(s);
sb.Append(",");
}
sb.AppendLine();
}
return sb.ToString();
}
}
}
fengart 2008-08-06
  • 打赏
  • 举报
回复
但lz是select new {字段1=table.field2+"-"+table.field4,字段2=table.field3}
sp1234的方法就不行了
SlaughtChen 2008-08-05
  • 打赏
  • 举报
回复
值得学习
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
都是很好的建议! 值得学习
iceface57163 2008-04-28
  • 打赏
  • 举报
回复
这样做固然达到目的,当这个DataTable 很大时,但是效率知道考虑!
  • 打赏
  • 举报
回复
哦,其实对象的只读属性也当然应该可以转化到DataRow里边的。

删除那两行 isReadOnly 判断!
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace 你的namespace
{
public static class Extension
{

public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
if (!dp.IsReadOnly)
ret.Columns.Add(dp.Name, dp.PropertyType);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
if (!dp.IsReadOnly)
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}
}
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Data;

// 你的namespace

public static class Extension
{

}

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧