将 SqlCommand 输出转换为 List<MyType>? [英] Convert SqlCommand Output to List<MyType>?
本文介绍了将 SqlCommand 输出转换为 List<MyType>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用带有单个 SqlDbType.Structured 参数的 ADO.NET SqlCommand 将表值参数发送到 sproc.sproc 返回许多行,我需要将这些行放入 .将结果集(无论是来自 DataAdapter 还是 DataReader 位的 DataTable)转换为 List 的最佳方法是什么?
I am using an ADO.NET SqlCommand with a single SqlDbType.Structured parameter to send a table-valued parameter to a sproc. The sproc returns many rows, which I need to get into a strongly-Typed List of . What is the best way to convert the result set (whether DataTable from a DataAdapter or DataReader bits) into List?
谢谢.
推荐答案
您可以将 LINQ 与 DataReader 一起使用:
You can use LINQ with a DataReader:
var list = reader.Cast<IDataRecord>()
.Select(dr => new YourType { Name = dr.GetString(0), ... })
.ToList();
这篇关于将 SqlCommand 输出转换为 List<MyType>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文