如何将LINQ结果转换为DATATABLE? [英] How to Convert a LINQ result to DATATABLE?

查看:141
本文介绍了如何将LINQ结果转换为DATATABLE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将LINQ表达式的结果转换为 DataTable 而不必逐步遍历每个元素?

Is there any way to convert the result of a LINQ expression to a DataTable without stepping through each element?

推荐答案

没有方法可以通过每个元素步进创建它。 Linq表达式在需要时进行评估,以便逐步遍历每一行(用于匹配和选择)。

Nope there is no way to create it without stepping through each element. The Linq expression is evaluated when needed so it will step through each row (for matching and selection).

我认为您应该尝试使用 DataTable。 Select() MSDN链接)方法,因为它返回可以添加到新表的 DataRow 对象的数组,如下所示:

I think you should try using DataTable.Select() (MSDN link) method instead as it returns array of DataRow objects that you can add to new table as follows:

var rows = [ORIGINAL DATA TABLE].Select("id>5");

var dtb=[ORIGINAL DATA TABLE].Clone();

foreach(DataRow r in rows)
{
    var newRow = dtb.NewRow();
    newRow.ItemArray = r.ItemArray;
    dtb.Rows.Add(newRow);//I'm doubtful if you need to call this or not
}

这篇关于如何将LINQ结果转换为DATATABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆