網站首頁 編程語言 正文
最近在項目中使用了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-01-28 手把手教你用python繪制熱度圖(heatmap)_python
- 2023-10-12 ant-design的Input輸入框取消選擇后的藍色背景以及如何取消提示
- 2022-04-27 Shell獲取路徑操作(dirname?$0?pwd)的實現_linux shell
- 2022-07-13 k8s 之 kubectl 提示 “The connection to the server loc
- 2022-05-05 深入講解下Rust模塊使用方式_相關技巧
- 2022-12-03 .Net?Core和RabbitMQ限制循環消費的方法_實用技巧
- 2022-06-07 FreeRTOS實時操作系統結構示例_操作系統
- 2021-12-10 C#?Quartzs定時器的使用教程_C#教程
- 最近更新
-
- 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同步修改后的遠程分支