天道酬勤,学无止境

datacolumncollection

How to remove DataColumn from DataTable programmatically

I have a code foreach (DataColumn dataTableCol in this.dataTable.Columns) { bool columnFound = false; foreach (GRTColumnView uiColumn in descriptor.UIColumns) { if (dataTableCol.ColumnName.Equals(uiColumn.Name)) { columnFound = true; break; } } if (!columnFound) { if (this.dataTable.Columns.Contains(dataTableCol.ColumnName)) this.dataTable.Columns.Remove(dataTableCol.ColumnName); } } I want to remove some "things" from collection if they aren't found in another collection. When I run the above program, I get Iteration may not execute as collection was modified "Collection was modified" - Coz

2021-11-24 11:05:09    分类:问答    c#   collections   datatable   datacolumn   datacolumncollection

使用 LINQ 查询 DataColumnCollection(Querying DataColumnCollection with LINQ)

问题 我正在尝试对 DataTable 的 Columns 属性执行简单的 LINQ 查询: from c in myDataTable.Columns.AsQueryable() select c.ColumnName 但是,我得到的是: 找不到源类型“System.Linq.IQueryable”的查询模式的实现。 未找到“选择”。 考虑明确指定范围变量“c”的类型。 如何让 DataColumnCollection 与 LINQ 配合使用? 回答1 怎么样: var x = from c in dt.Columns.Cast<DataColumn>() select c.ColumnName; 回答2 您还可以使用: var x = from DataColumn c in myDataTable.Columns select c.ColumnName 根据 Enumerable.Cast<TResult> 方法 MSDN 文章,它将有效地执行与 Dave 的代码相同的操作:“在查询表达式中,显式类型的迭代变量转换为 Cast(IEnumerable) 的调用”。 回答3 使用 Linq 方法语法: var x = myDataTable.Columns.Cast<DataColumn>().Select(c => c.ColumnName);

2021-06-11 23:57:16    分类:技术分享    c#   linq   datatable   asqueryable   datacolumncollection

Querying DataColumnCollection with LINQ

I'm trying to perform a simple LINQ query on the Columns property of a DataTable: from c in myDataTable.Columns.AsQueryable() select c.ColumnName However, what I get is this: Could not find an implementation of the query pattern for source type 'System.Linq.IQueryable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'c'. How can I get the DataColumnCollection to play nice with LINQ?

2021-05-15 14:36:00    分类:问答    c#   linq   datatable   asqueryable   datacolumncollection