如何设置内联集合?

发布于 2025-01-01 02:15:25 字数 198 浏览 0 评论 0原文

例如:

DataTable table = new DataTable() 
{ 
  Columns = new DataColumnCollection(
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     })
});

For example:

DataTable table = new DataTable() 
{ 
  Columns = new DataColumnCollection(
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     })
});

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

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

发布评论

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

评论(6

宛菡 2025-01-08 02:15:25

您正在谈论 集合初始化程序
C# 3 中添加的功能。它是这样完成的:

DataTable table = new DataTable() 
{ 
    Columns = 
    { 
        new DataColumn("col1"), 
        new DataColumn("col2")
    }
};

这不会调用集合构造函数,它使用 DataTable 中已存在的集合。

这是 Columns.Add() 的简写,因此不需要 Columns 有 setter。

您与问题中的代码非常接近!

You are talking about the Collection Initialiser
feature added in C# 3. It is done like this:

DataTable table = new DataTable() 
{ 
    Columns = 
    { 
        new DataColumn("col1"), 
        new DataColumn("col2")
    }
};

This does not call a collection constructor, it uses the collection which already exists in the DataTable.

This is short-hand for Columns.Add(), so it doesn't require Columns to have a setter.

You were so close with the code in your question!

亢潮 2025-01-08 02:15:25

Columns 属性没有 setter,因此您只能修改它。

怎么样:

DataTable table = new DataTable();
table.Columns.AddRange(new[] { new DataColumn("col1"), new DataColumn("col2") });

如果您想使用 lambda 中的一条语句:

DataTable table = (() => {
    var table = new DataTable();
    table.Columns.AddRange(new[] { new DataColumn("col1"),
                                   new DataColumn("col2") });
    return table;})();

The Columns property does not have a setter so you can only modify it.

How about this:

DataTable table = new DataTable();
table.Columns.AddRange(new[] { new DataColumn("col1"), new DataColumn("col2") });

If you want to do with one statement in a lambda:

DataTable table = (() => {
    var table = new DataTable();
    table.Columns.AddRange(new[] { new DataColumn("col1"),
                                   new DataColumn("col2") });
    return table;})();
橘虞初梦 2025-01-08 02:15:25

您可能需要删除 DataColumnCollection 的集合初始值设定项周围的括号,并删除不匹配的最终 )

不过,这些都是语法问题。根本问题是 Columns 属性没有 setter,并且 DataColumnCollection 没有公共构造函数。

基本上,您必须实例化然后调用 .Columns.Add()

如果这是您必须在代码中做很多事情的事情,您可以创建帮助器类来为您提供更友好的语法:

DataTable table = DataTableFactory.CreateTableWithColumns("col1", "col2");

You probably need to remove the paretheses around that collection initializer for DataColumnCollection, and remove the unmatched, final )

Those are syntactical issues, though. The underlying problems are that the Columns property has no setter, and the DataColumnCollection has no public constructor.

Basically, you have to instantiate and then call .Columns.Add().

If this is something you have to do a lot in your code, you could create helper classes that would give you friendlier syntax:

DataTable table = DataTableFactory.CreateTableWithColumns("col1", "col2");
走过海棠暮 2025-01-08 02:15:25

这不起作用的原因有 2 个:

1) Columns 属性是只读的
2) DataColumnCollection 类没有接受列集合来初始化它的构造函数。
您能做的最好的事情就是在一行中创建表格并在另一行中添加列:

DataTable table = new DataTable();
table.Columns.AddRange( new []
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     });

为了回答您的其他问题,IF Columns 有一个设置器和IF DataColumnCollection 在其构造函数中接受列,语法为:

DataTable table = new DataTable() 
{ 
  Columns = new DataColumnCollection( new DataColumn[]
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     })
});

There are 2 reasons why this won't work:

1) the Columns property is read-only
2) the DataColumnCollection class does not have a constructor that accepts a collection of columns to initialize it.
Best you can do is create the table in one line and add the columns in another:

DataTable table = new DataTable();
table.Columns.AddRange( new []
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     });

To answer your other question, IF Columns had a setter and IF DataColumnCollection accepted columns in its constructor the syntax would be:

DataTable table = new DataTable() 
{ 
  Columns = new DataColumnCollection( new DataColumn[]
     { 
         new DataColumn("col1"), 
         new DataColumn("col2")
     })
});
音盲 2025-01-08 02:15:25

DataColumnCollection 类没有构造函数,因此您无法手动创建实例。编译器的错误消息应该是非常不言自明的,大致如下:

类型“System.Data.DataColumnCollection”没有定义构造函数

您可以使用 Add() 方法将列添加到 DataTable.Columns 集合中:

DataTable table = new DataTable();
table.Columns.Add(new DataColumn("col1"));
table.Columns.Add(new DataColumn("col2"));

The class DataColumnCollection has no constructor so you can't manually create an instance. The compiler's error message should be pretty self-explanatory, saying something along the lines of:

The type 'System.Data.DataColumnCollection' has no constructors defined

You can add columns to the DataTable.Columns collection by using the Add() method:

DataTable table = new DataTable();
table.Columns.Add(new DataColumn("col1"));
table.Columns.Add(new DataColumn("col2"));
思慕 2025-01-08 02:15:25

您不能使用该语法,因为 Columns 属性是只读的。我会使用加布建议的技术。

You can't use that syntax as the Columns property is readonly. I'd use the technique suggested by Gabe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文