- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
示例代码:
DataTable table = new DataTable();
// ...
// insert column to table
table.Columns.Add("name");
// ...
// insert value to table
foreach (DataRow row in table.Rows) {
row["name"];
row.Field<string>("name");
}
我的问题是:
row["name"]
有区别吗?和 row.Field<string>("name")
?当然,第二种方式将值转换为某种类型,但是还有其他区别吗? 最佳答案
参见 Remarks section ,那里描述的主要区别:
The DataSet class represents null values with the Value instance of the DBNull class. A Language-Integrated Query (LINQ) expression that accessed a column with a null value would generate a InvalidCastException at run time. Additionally, DataSet does not support nullable types. The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is Value, the returned nullable type will have a value of null.
If the value of the specified DataColumn is null and T is a reference type or nullable type, the return type will be null. The Field method will not return Value.
The Field method does not perform type conversions. If type conversion is required, you should first obtain the column value by using the Field method. The column value should then be converted to another type.
最后一段提出了一个观点,因为我经常看到数字在数据库中存储为字符串,因此 varchar
至 int
数据检索需要转换,因此在这种情况下使用 DataColumn 更好,例如:
int test = row.Field<int>("Test"); // InvalidCastException
int test = Convert.ToInt32(row["Test"]); // Works like a charm
DataRowExtensions.Field<T> Method (DataRow, String)
首次出现在 .NET 3.5 中,它“提供对指定行中每个列值的强类型访问。Field 方法还支持可空类型。”
阿法克,row["name"]
返回 object
, row.Field<string>("name")
返回 String
.我们不应该比较苹果和梨,因此你应该问哪个更好:
row["name"].ToString()
对比row.Field<string>("name")
答案是:它们是一样的。
关于c# - 从 DataRow 获取值(value)的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7104675/
这个问题在这里已经有了答案: final keyword in method parameters [duplicate] (9 个回答) 关闭 8 年前。 在此示例中,声明 Object fina
我的目标:是通过我的函数更新字段获取选定值并使用函数输出值运行它。 问题:当我从列表中选择值时,它不会触发函数,也不会更新字段。 感谢您的帮助。 HTML 12 14 16 18 20 22 24
我有一本具有这种形式的字典: myDict = {'foo': bar, 'foobar baz': qux} 现在,我想拆分字典键中的空格,使其成为下一个键并获取值(重复)。 myDictRev1
vector a; vector b; int temp_holder; cout > temp_holder) a.push_back(temp_holder); cout > temp_h
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我
我正在使用 jquery ui 日期选择器来获取 fromDate 和 toDate 以下是from日期的代码 $("#from_date").datepicker({
我是一名优秀的程序员,十分优秀!