Linq中ToList()和CopyToDataTable()用法詳解
最近在項(xiàng)目中使用了Linq,想把Linq的查詢結(jié)果直接轉(zhuǎn)換成DataTable對(duì)象,通過查找發(fā)現(xiàn)Linq有一個(gè)CopyToDataTable<T>的泛型方法,該方法只能在T是DataRow的情況下使用,發(fā)現(xiàn)了這個(gè)方法以后就直接在項(xiàng)目中使用了,但是在使用的過程中發(fā)現(xiàn),如果Linq的查詢結(jié)果不包含任何DataRow對(duì)象的時(shí)候,使用CopyToDataTable()方法會(huì)報(bào)錯(cuò),代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace CopyToDataTableDemo
{
class Program
{
static void Main(string[] args)
{
string strConn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strConn))
{
string strSQL = "SELECT * FROM Product";
SqlCommand cmd = new SqlCommand(strSQL, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
try
{
DataTable dt = new DataTable();
adapter.Fill(dt);
//CopyToDataTable()
DataTable dtTemp = dt.AsEnumerable().Where<DataRow>(p =>
{
return p["ProductId"].ToString().Trim().Equals("4");
}).CopyToDataTable();
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
}
}
}報(bào)錯(cuò)信息如下:

該錯(cuò)誤信息說明如果Linq的查詢結(jié)果不包含任何DataRow對(duì)象的時(shí)候,使用該方法會(huì)報(bào)錯(cuò),那么怎么將Linq的查詢結(jié)果轉(zhuǎn)換成DataTable使用呢?
繼續(xù)查詢Linq的方法,發(fā)現(xiàn)Linq還有一個(gè)ToList()的方法,使用該方法可以解決Linq查詢結(jié)果不包含任何DataRow對(duì)象時(shí)報(bào)錯(cuò)的問題,代碼修改如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace CopyToDataTableDemo
{
class Program
{
static void Main(string[] args)
{
string strConn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strConn))
{
string strSQL = "SELECT * FROM Product";
SqlCommand cmd = new SqlCommand(strSQL, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
try
{
DataTable dt = new DataTable();
adapter.Fill(dt);
//CopyToDataTable()
// 當(dāng)LINQ的查詢結(jié)果不包含任何DataRow對(duì)象的時(shí)候會(huì)報(bào)錯(cuò)
//DataTable dtTemp = dt.AsEnumerable().Where<DataRow>(p =>
//{
// return p["ProductId"].ToString().Trim().Equals("4");
//}).CopyToDataTable();
//ToList()
List<DataRow> list = dt.AsEnumerable().Where<DataRow>(p =>
{
return p["ProductId"].ToString().Trim().Equals("4");
}).ToList();
if (list.Count > 0)
{
DataTable dtTemp = dt.Clone();
// 循環(huán)遍歷list轉(zhuǎn)換成DataTable
list.ForEach(p =>
{
dtTemp.Rows.Add(p.ItemArray);
});
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
}
}
}使用ToList()方法就可以解決該報(bào)錯(cuò)問題了。
到此這篇關(guān)于Linq中ToList()和CopyToDataTable()用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net(c#)復(fù)數(shù)類(復(fù)數(shù)加減乘除四則運(yùn)算)
asp.net(c#)復(fù)數(shù)類(復(fù)數(shù)加減乘除四則運(yùn)算)...2007-06-06
.Net?Core使用Logger實(shí)現(xiàn)log寫入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實(shí)現(xiàn)log寫入本地文件系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄
這篇文章主要介紹了.NET?API?接口數(shù)據(jù)傳輸加密最佳實(shí)踐記錄,我們?cè)谧?Api?接口時(shí),相信一定會(huì)有接觸到要給傳輸?shù)恼?qǐng)求?body?的內(nèi)容進(jìn)行加密傳輸。其目的就是為了防止一些敏感的內(nèi)容直接被?UI?層查看或篡改,需要的朋友可以參考下2022-10-10
asp.net Repeater取得CheckBox選中的某行某個(gè)值
Repeater取得CheckBox選中的某行某個(gè)值的實(shí)現(xiàn)代碼2008-07-07
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較...2007-08-08
詳解Asp.net Core 使用Redis存儲(chǔ)Session
本篇文章主要介紹了Asp.net Core 使用Redis存儲(chǔ)Session ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2016-12-12

