CopyToDataTable 不起作用....生成错误

发布于 2024-11-02 21:32:55 字数 1993 浏览 0 评论 0原文

我从 msdn 获得了一个示例,代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LINQTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Item[] items = new Item[] 
            { new Book{Id = 1, Price = 13.50, Genre = "Comedy", Author = "Gustavo Achong"}, 
              new Book{Id = 2, Price = 8.50, Genre = "Drama", Author = "Jessie Zeng"},
              new Movie{Id = 1, Price = 22.99, Genre = "Comedy", Director = "Marissa Barnes"},
              new Movie{Id = 1, Price = 13.40, Genre = "Action", Director = "Emmanuel Fernandez"}};

                        // Load into an existing DataTable, expand the schema and
                        // autogenerate a new Id.
                        DataTable table = new DataTable();
                        DataColumn dc = table.Columns.Add("NewId", typeof(int));
                        dc.AutoIncrement = true;
                        table.Columns.Add("ExtraColumn", typeof(string));

                        var query = from i in items
                                    where i.Price > 9.99
                                    orderby i.Price
                                    select new { i.Price, i.Genre };

                       query.CopyToDataTable(table, LoadOption.PreserveChanges);

                    }
    }

    public class Item
    {
        public int Id { get; set; }
        public double Price { get; set; }
        public string Genre { get; set; }
    }

    public class Book : Item
    {
        public string Author { get; set; }
    }

    public class Movie : Item
    {
        public string Director { get; set; }
    }
}

我收到 CopyToDataTable 错误。我错过了什么吗?

I've got a sample from msdn and the code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LINQTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Item[] items = new Item[] 
            { new Book{Id = 1, Price = 13.50, Genre = "Comedy", Author = "Gustavo Achong"}, 
              new Book{Id = 2, Price = 8.50, Genre = "Drama", Author = "Jessie Zeng"},
              new Movie{Id = 1, Price = 22.99, Genre = "Comedy", Director = "Marissa Barnes"},
              new Movie{Id = 1, Price = 13.40, Genre = "Action", Director = "Emmanuel Fernandez"}};

                        // Load into an existing DataTable, expand the schema and
                        // autogenerate a new Id.
                        DataTable table = new DataTable();
                        DataColumn dc = table.Columns.Add("NewId", typeof(int));
                        dc.AutoIncrement = true;
                        table.Columns.Add("ExtraColumn", typeof(string));

                        var query = from i in items
                                    where i.Price > 9.99
                                    orderby i.Price
                                    select new { i.Price, i.Genre };

                       query.CopyToDataTable(table, LoadOption.PreserveChanges);

                    }
    }

    public class Item
    {
        public int Id { get; set; }
        public double Price { get; set; }
        public string Genre { get; set; }
    }

    public class Book : Item
    {
        public string Author { get; set; }
    }

    public class Movie : Item
    {
        public string Director { get; set; }
    }
}

I'm getting error for CopyToDataTable. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2024-11-09 21:32:56

您必须将对象转换为数据行。

试试这个:

    query.Select(el =>
                     {
                         DataRow row =
                             table.NewRow();
                         row["ExtraColumn"] = el.Genre;
                         return row;
                     }
        ).CopyToDataTable(table, LoadOption.PreserveChanges);

You'll have to cast your object to a datarow.

Try this:

    query.Select(el =>
                     {
                         DataRow row =
                             table.NewRow();
                         row["ExtraColumn"] = el.Genre;
                         return row;
                     }
        ).CopyToDataTable(table, LoadOption.PreserveChanges);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文