網站首頁 編程語言 正文
最近在項目中使用了Linq,想把Linq的查詢結果直接轉換成DataTable對象,通過查找發現Linq有一個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(p => { return p["ProductId"].ToString().Trim().Equals("4"); }).CopyToDataTable(); } catch (Exception ex) { } finally { conn.Close(); } } } } }
報錯信息如下:
該錯誤信息說明如果Linq的查詢結果不包含任何DataRow對象的時候,使用該方法會報錯,那么怎么將Linq的查詢結果轉換成DataTable使用呢?
繼續查詢Linq的方法,發現Linq還有一個ToList()的方法,使用該方法可以解決Linq查詢結果不包含任何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() // 當LINQ的查詢結果不包含任何DataRow對象的時候會報錯 //DataTable dtTemp = dt.AsEnumerable().Where(p => //{ // return p["ProductId"].ToString().Trim().Equals("4"); //}).CopyToDataTable(); //ToList() List list = dt.AsEnumerable().Where (p => { return p["ProductId"].ToString().Trim().Equals("4"); }).ToList(); if (list.Count > 0) { DataTable dtTemp = dt.Clone(); // 循環遍歷list轉換成DataTable list.ForEach(p => { dtTemp.Rows.Add(p.ItemArray); }); } } catch (Exception ex) { } finally { conn.Close(); } } } } }
使用ToList()方法就可以解決該報錯問題了。
原文鏈接:https://www.cnblogs.com/dotnet261010/p/8195840.html
相關推薦
- 2023-02-06 Python?tkinter庫實現登錄注冊基本功能_python
- 2022-04-02 Python數據結構之雙向鏈表詳解_python
- 2022-07-09 docker 中進程的信號
- 2022-07-16 四種解決Nginx出現403 forbidden 報錯的方法
- 2022-11-17 React中實現插槽效果的方案詳解_React
- 2023-06-17 關于生產消費者模型中task_done()的具體作用_python
- 2022-09-18 Go語言包管理工具Godep的用法_Golang
- 2023-03-16 redis刪除hash的實現方式_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支