Generic Field and SetField methods (LINQ to DataSet)

Source: Internet
Author: User

The LINQ to DataSet provides an extension method for the DataRow class to access column values: The Field method and the SetField method. These methods make it easier for developers to access column values, especially null values. The DataSet uses value to represent null values, and LINQ uses the nullable types that are introduced in the. NET Framework 2.0 support.Using pre-existing column accessors in a DataRow requires that the returned object be cast to the appropriate type.If a specific field in a DataRow can be null, you must display the check null value, because returning value and implicitly casting it to another type throws InvalidCastException.In the following example, if you do not use the IsNull method to check for a Null value, an exception is thrown when the indexer returns value and attempts to cast it to a string.

DataSet ds =NewDataSet ();d S. Locale=CultureInfo.InvariantCulture; FillDataSet (DS);D atatable Products= ds. tables["Product"];varquery = fromProductinchProducts . AsEnumerable ()where!product. IsNull ("Color") &&        (string) product["Color"] =="Red"    Select New{Name= product["Name"], ProductNumber= product["ProductNumber"], ListPrice= product["ListPrice"]    };foreach(varProductinchquery) {Console.WriteLine ("Name: {0}", product.    Name); Console.WriteLine ("Product number: {0}", product.    ProductNumber); Console.WriteLine ("List Price: ${0}", product.    ListPrice); Console.WriteLine ("");}

The Field method provides access to the values of the DataRow column, and SetField sets the column values in the DataRow.both the Field method and the SetField method can handle nullable types, so you do not have to check for null values as in the previous example. Both of these methods are generic methods, so you do not have to cast the return type.

The following example uses the Field method.

//Fill the DataSet.DataSet ds =NewDataSet ();d S. Locale=CultureInfo.InvariantCulture; FillDataSet (DS);D atatable Products= ds. tables["Product"];varquery = fromProductinchProducts . AsEnumerable ()whereProduct. field<string> ("Color") =="Red"    Select New{Name= Product. field<string> ("Name"), ProductNumber= Product. field<string> ("ProductNumber"), ListPrice= Product. Field<decimal> ("ListPrice")    };foreach(varProductinchquery) {Console.WriteLine ("Name: {0}", product.    Name); Console.WriteLine ("Product number: {0}", product.    ProductNumber); Console.WriteLine ("List Price: ${0}", product.    ListPrice); Console.WriteLine ("");}

Note that the Field method and the generic parameter of the SetField methodTThe data type specified in must match the type of the underlying value.Otherwise, a InvalidCastException exception is thrown. The specified column name must also match the column name in the DataSet, or ArgumentException will be thrown. in both cases, the exception is thrown when the runtime data enumeration is performed during the execution of the query.

The SetField method itself does not perform any type conversions.However, this does not mean that type conversions do not occur. The SetField method exposes the ADO 2.0 behavior of the DataRow class. A type conversion can be performed by a DataRow object, and the converted value is then saved to the DataRow object.

Generic Field and SetField method (LINQ to DataSet)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.