TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# DataRow Field Method: Cast DataTable Cell

Use the Field method on the DataRow class to cast DataTable cells to a specific type.
DataRow Field. The Field method on DataRow accesses a field. It is an extension method. It is found in the System.Data namespace.
Notes, method. Field() is a generic method—this means it returns typed data. It makes field access easier. We can avoid complicated casting expressions and exception handling.DataRow
An example. DataTable can be enumerated with the foreach-loop. Accessing the fields themselves is somewhat more complicated. The fields can have different types.DataTable

Types: The DataTable in the example has cells with types of int, string and DateTime.

Foreach: The example uses the foreach-loop. In the statement list it assigns variables to the results of the Field extension method.

DataTable Foreach Loop

Tip: The Field extension method receives a type parameter. It has several parameter list overloads.

Extension

Assignments: The weight, name, code, and date variables are assigned to the results of the Field method with different parameters.

C# program that uses DataRow Field extension method using System; using System.Data; class Program { static void Main() { // // Loop over DataTable rows and call the Field extension method. // foreach (DataRow row in GetTable().Rows) { // Get first field by column index. int weight = row.Field<int>(0); // Get second field by column name. string name = row.Field<string>("Name"); // Get third field by column index. string code = row.Field<string>(2); // Get fourth field by column name. DateTime date = row.Field<DateTime>("Date"); // Display the fields. Console.WriteLine("{0} {1} {2} {3}", weight, name, code, date); } } static DataTable GetTable() { DataTable table = new DataTable(); // Create DataTable table.Columns.Add("Weight", typeof(int)); // Add four columns table.Columns.Add("Name", typeof(string)); table.Columns.Add("Code", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); table.Rows.Add(57, "Koko", "A", DateTime.Now); // Add five rows table.Rows.Add(130, "Fido", "B", DateTime.Now); table.Rows.Add(92, "Alex", "C", DateTime.Now); table.Rows.Add(25, "Charles", "D", DateTime.Now); table.Rows.Add(7, "Candy", "E", DateTime.Now); return table; } } Output 57 Koko A 7/14/2009 6:20:48 AM 130 Fido B 7/14/2009 6:20:48 AM 92 Alex C 7/14/2009 6:20:48 AM 25 Charles D 7/14/2009 6:20:48 AM 7 Candy E 7/14/2009 6:20:48 AM
Parameters. There are 2 parameter lists on each method invocation to Field. The first parameter to the method is enclosed in the brackets. This is called a type parameter.

Tip: A type parameter indicates what type (such as int or string) you want the method to cast the result to.

Generic Class, Method

Note: The second parameter list indicates the name or location of the field by its column identifier.

Internals. How does Field() work internally? The DataRowExtensions class is an extension method class located in the System.Data.DataSetExtensions.dll file.

And: When you use the methods such as Field in this class, the metadata loader will store this additional DLL in memory.

Info: Field() will internally invoke a generic method that validates the parameter. It then uses a generic method to unbox the object.

Field, performance. Because no copying of the actual data will take place, the Field extension method will have adequate performance. A lower-level approach would be faster.
A summary. We looked at the Field extension method found in the System.Data namespace and the DataSetExtensions class. We cast the cells in a DataTable by a column identifier.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf