将DataTable转换为对象列表 [英] Converting DataTable to List of objects

查看:67
本文介绍了将DataTable转换为对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的DataTable转换为对象列表,但我收到错误消息"指定的转换无效"  当我现在尝试使用System从数据库

I am trying to convert my DataTable to a list of objects but I am getting an error saying "specified cast is not valid"  when I am now trying to create a list from the data I got from the database

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            myConnectionString = "Data Source=" + ".\\SQLServerInstance" + @";Initial Catalog = " + "DatabaseName" + @";Integrated Security=SSPI;";
        }
        
        string myConnectionString;
        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection myGetTaxTypeConnection = new SqlConnection(myConnectionString);
            SqlDataAdapter myGetTaxTypeDataAdapter = new SqlDataAdapter("SELECT * FROM Settings_TaxType", myGetTaxTypeConnection);
            SqlCommandBuilder myGetTaxTypeCommandBuilder = new SqlCommandBuilder(myGetTaxTypeDataAdapter);
            DataTable myGetTaxTypeDataTable = new DataTable();
            myGetTaxTypeDataTable.Reset();
            myGetTaxTypeDataAdapter.Fill(myGetTaxTypeDataTable);

            List<myTaxType> myTaxTypesList = (from myDataRow in myGetTaxTypeDataTable.AsEnumerable()
            select new myTaxType()
            {
                    TaxTypeID = myDataRow.Field<int>("TaxTypeID"),
                    TaxType = myDataRow.Field<string>("TaxType"),
                    TaxRate = myDataRow.Field<decimal>("TaxRate"),
                    TaxStartDate = myDataRow.Field<DateTime>("TaxStartDate"),
                    TaxEndDate = myDataRow.Field<DateTime>("TaxEndDate"),
                    Discontinued = myDataRow.Field<bool>("Discontinued")
            }).ToList();
        }
        class myTaxType
        {
            public int TaxTypeID { get; set; }
            public string TaxType { get; set; }
            public decimal TaxRate { get; set; }
            public DateTime TaxStartDate { get; set; }
            public DateTime TaxEndDate { get; set; }
            public bool Discontinued { get; set; }
        }        
    }
}


当这种方法不起作用时,我尝试了一种不同的方法,这样它也给了相同的错误

When this method did not work I tried a different approach like this and it is also giving the same error

List<TaxType> listName = myGetTaxTypeDataTable.AsEnumerable().Select(m => new TaxType()
{
    TaxTypeID = m.Field<int>("TaxTypeID"),
    myTaxType = m.Field<string>("TaxType"),
    TaxRate = m.Field<decimal>("TaxRate"),
    TaxStartDate = m.Field<DateTime>("TaxStartDate"),
    TaxEndDate = m.Field<DateTime>("TaxEndDate")
}).ToList();


请帮助我。我不想循环



如果您认为可以实现它

Please help me . I dont want to loop


If you think it you can achieve it

推荐答案

这是表的结构我从
获取数据
This is the structure of the table I am getting the data from
CREATE TABLE [dbo].[Settings_TaxType](
	[TaxTypeID] [bigint] IDENTITY(1,1) NOT NULL,
	[TaxType] [nvarchar](40) NOT NULL,
	[TaxRate] [decimal](18, 3) NULL,
	[TaxStartDate] [datetime] NULL,
	[TaxEndDate] [datetime] NULL,
	[Discontinued] [bit] NOT NULL CONSTRAINT [DF_TaxType_Discontiued]  DEFAULT ((0)),
 CONSTRAINT [PK_TaxType] PRIMARY KEY CLUSTERED 
(
	[TaxTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO








这篇关于将DataTable转换为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆