Linq中ToList()和CopyToDataTable()用法詳解
最近在項目中使用了Linq,想把Linq的查詢結(jié)果直接轉(zhuǎn)換成DataTable對象,通過查找發(fā)現(xiàn)Linq有一個CopyToDataTable<T>的泛型方法,該方法只能在T是DataRow的情況下使用,發(fā)現(xiàn)了這個方法以后就直接在項目中使用了,但是在使用的過程中發(fā)現(xiàn),如果Linq的查詢結(jié)果不包含任何DataRow對象的時候,使用CopyToDataTable()方法會報錯,代碼如下:
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();
}
}
}
}
}報錯信息如下:

該錯誤信息說明如果Linq的查詢結(jié)果不包含任何DataRow對象的時候,使用該方法會報錯,那么怎么將Linq的查詢結(jié)果轉(zhuǎn)換成DataTable使用呢?
繼續(xù)查詢Linq的方法,發(fā)現(xiàn)Linq還有一個ToList()的方法,使用該方法可以解決Linq查詢結(jié)果不包含任何DataRow對象時報錯的問題,代碼修改如下:
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對象的時候會報錯
//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()方法就可以解決該報錯問題了。
到此這篇關(guān)于Linq中ToList()和CopyToDataTable()用法的文章就介紹到這了。希望對大家的學(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實現(xiàn)log寫入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實現(xiàn)log寫入本地文件系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
.NET?API?接口數(shù)據(jù)傳輸加密最佳實踐記錄
這篇文章主要介紹了.NET?API?接口數(shù)據(jù)傳輸加密最佳實踐記錄,我們在做?Api?接口時,相信一定會有接觸到要給傳輸?shù)恼埱?body?的內(nèi)容進(jìn)行加密傳輸。其目的就是為了防止一些敏感的內(nèi)容直接被?UI?層查看或篡改,需要的朋友可以參考下2022-10-10
asp.net Repeater取得CheckBox選中的某行某個值
Repeater取得CheckBox選中的某行某個值的實現(xiàn)代碼2008-07-07
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較...2007-08-08
詳解Asp.net Core 使用Redis存儲Session
本篇文章主要介紹了Asp.net Core 使用Redis存儲Session ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。2016-12-12

