使用 linq 将列添加到当前数据表
我们可以添加一个新的列,该列是在现有数据表中计算的列,
dt1.Rows.Add(new string[] { "1", "a", "a1" });
dt1 = (from r in dt1.AsEnumerable()
select new {
col3 = r.Field<string>("col1") + r.Field<string>("col2")
});
结果数据表应该包含新列
Can we add a new Column which is calculated Column in a existing DataTable
dt1.Rows.Add(new string[] { "1", "a", "a1" });
dt1 = (from r in dt1.AsEnumerable()
select new {
col3 = r.Field<string>("col1") + r.Field<string>("col2")
});
resulting DataTable should contain the new Column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,但不能通过 LINQ:
您还可以使用 LINQ 方法创建投影,但这不会更改表(因此分配给
dt1
是一个错误) 。从技术上讲,您可以编写一个表达式解析器来为您编写.Expression
,但我不认为这会是一个很好的时间投资。Yes, but not via LINQ:
You can also use the LINQ approach to create a projection, but that isn't changing the table (so assigning to
dt1
is a mistake). Technically you could write an expression parser to write the.Expression
for you, but I can't see that this would be a good investment of time.这是减少 For/ForEach 循环的替代解决方案,
Here is an alternate solution to reduce For/ForEach looping,