Linq中ToList()和CopyToDataTable()用法詳解
最近在項(xiàng)目中使用了Linq,想把Linq的查詢結(jié)果直接轉(zhuǎn)換成DataTable對(duì)象,通過(guò)查找發(fā)現(xiàn)Linq有一個(gè)CopyToDataTable<T>的泛型方法,該方法只能在T是DataRow的情況下使用,發(fā)現(xiàn)了這個(gè)方法以后就直接在項(xiàng)目中使用了,但是在使用的過(guò)程中發(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ò)誤信息說(shuō)明如果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ò)的問(wèn)題,代碼修改如下:
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ò)問(wèn)題了。
到此這篇關(guān)于Linq中ToList()和CopyToDataTable()用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net中關(guān)于dropdwonlist無(wú)法獲得值問(wèn)題
用dropdwonlist綁定了一個(gè)數(shù)據(jù)源做選擇,但是當(dāng)提交時(shí),用控件屬性無(wú)法獲得相應(yīng)的值,打印出來(lái)每次都是顯示的第一個(gè)值2011-11-11ASP.NET中的DataGridView綁定數(shù)據(jù)和選中行刪除功能具體實(shí)例
廢話就不多說(shuō)了,都說(shuō).NET是托控件的,我就托給你們看,這個(gè)博文主要講 DataGridView 的數(shù)據(jù)綁定,和選中行刪除功能2013-12-12Asp.Net實(shí)現(xiàn)的通用分頁(yè)函數(shù)
這篇文章主要介紹了Asp.Net實(shí)現(xiàn)的通用分頁(yè)函數(shù),結(jié)合實(shí)例形勢(shì)分析了asp.net分頁(yè)函數(shù)的功能,定義及使用技巧,需要的朋友可以參考下2016-04-04如何給asp.net core寫個(gè)簡(jiǎn)單的健康檢查
這篇文章主要給大家介紹了關(guān)于如何給asp.net core寫個(gè)簡(jiǎn)單的健康檢查的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05ASP.NET實(shí)現(xiàn)單點(diǎn)登陸(SSO)適用于多種情況
這篇文章主要介紹了ASP.NET在不同情況下實(shí)現(xiàn)單點(diǎn)登陸(SSO)的方法,在同主域但不同子域之間實(shí)現(xiàn)單點(diǎn)登陸等等2014-09-09asp.net動(dòng)態(tài)加載自定義控件的方法
這篇文章主要介紹了asp.net動(dòng)態(tài)加載自定義控件的方法,涉及asp.net動(dòng)態(tài)加載控件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04異步 HttpContext.Current實(shí)現(xiàn)取值的方法(解決異步Application,Session,Cache.
在一個(gè)項(xiàng)目中,為了系統(tǒng)執(zhí)行效率更快,把一個(gè)經(jīng)常用到的數(shù)據(jù)庫(kù)表通過(guò)dataset放到Application中,發(fā)現(xiàn)在異步實(shí)現(xiàn)中每一次都會(huì)出現(xiàn)HttpContext.Current為null的異常,后來(lái)在網(wǎng)上查了好多資料,發(fā)現(xiàn)問(wèn)這個(gè)問(wèn)題的人多,回答的少2009-07-07