共用方式為


泛型 Field 和 SetField 方法 (LINQ to DataSet)

更新: November 2007

LINQ to DataSet 會提供擴充方法給 DataRow 類別 (Class),以便存取資料行值:Field 方法和 SetField 方法。這些方法可讓開發人員更容易存取資料行值,尤其是與 Null 值相關的情況。DataSet 會使用 Value 來代表 Null 值,而 LINQ 則會使用 .NET Framework 2.0 中引進的可為 Null 的型別 (Nullable Type) 支援。如果使用 DataRow 中的現有資料行存取子,您就必須將傳回物件轉型為適當的型別。如果 DataRow 中的特定欄位可為 Null,您就必須明確檢查是否有 Null 值,因為傳回 Value 並將它隱含轉型為其他型別會擲回 InvalidCastException。在下列範例中,如果 IsNull 方法沒有用來檢查是否有 Null 值,而且索引子 (Indexer) 傳回了 Value 並嘗試將它轉型為 String,系統就會擲回例外狀況 (Exception)。

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim query = _
    From product In products.AsEnumerable() _
    Where product!Color IsNot DBNull.Value AndAlso product!Color = "Red" _
    Select New With _
       { _
           .Name = product!Name, _
           .ProductNumber = product!ProductNumber, _
           .ListPrice = product!ListPrice _
       }

For Each product In query
    Console.WriteLine("Name: " & product.Name)
    Console.WriteLine("Product number: " & product.ProductNumber)
    Console.WriteLine("List price: $" & product.ListPrice & vbNewLine)
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

var query =
    from product in products.AsEnumerable()
    where !product.IsNull("Color") &&
        (string)product["Color"] == "Red"
    select new
    {
        Name = product["Name"],
        ProductNumber = product["ProductNumber"],
        ListPrice = product["ListPrice"]
    };

foreach (var product in query)
{
    Console.WriteLine("Name: {0}", product.Name);
    Console.WriteLine("Product number: {0}", product.ProductNumber);
    Console.WriteLine("List price: ${0}", product.ListPrice);
    Console.WriteLine("");
}

Field 方法可讓您存取 DataRow 的資料行值而 SetField 會設定 DataRow 中的資料行值。由於 Field 方法和 SetField 方法都會處理可為 Null 的型別,因此您不需要明確檢查是否有 Null 值 (如同上一個範例)。此外,這兩種方法都是泛型方法,所以您不需要轉型傳回型別。

下列範例會使用 Field 方法。

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim query = _
    From product In products.AsEnumerable() _
    Where product.Field(Of String)("Color") = "Red" _
    Select New With _
       { _
           .Name = product.Field(Of String)("Name"), _
           .ProductNumber = product.Field(Of String)("ProductNumber"), _
           .ListPrice = product.Field(Of Decimal)("ListPrice") _
       }

For Each product In query
    Console.WriteLine("Name: " & product.Name)
    Console.WriteLine("Product number: " & product.ProductNumber)
    Console.WriteLine("List price: $ " & product.ListPrice & vbNewLine)
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

var query =
    from product in products.AsEnumerable()
    where product.Field<string>("Color") == "Red"
    select new
    {
        Name = product.Field<string>("Name"),
        ProductNumber = product.Field<string>("ProductNumber"),
        ListPrice = product.Field<Decimal>("ListPrice")
    };

foreach (var product in query)
{
    Console.WriteLine("Name: {0}", product.Name);
    Console.WriteLine("Product number: {0}", product.ProductNumber);
    Console.WriteLine("List price: ${0}", product.ListPrice);
    Console.WriteLine("");
}

請注意,Field 方法和 SetField 方法之泛型參數 T 中指定的資料型別必須與基礎值的型別相符。否則,系統將擲回 InvalidCastException 例外狀況。此外,指定的資料行名稱也必須與 DataSet 中的資料行名稱相符,否則系統將擲回 ArgumentException。在這兩種情況中,其例外狀況是在執行查詢的資料列舉執行階段中擲回。

SetField 方法本身不會執行任何型別轉換。不過,這並不表示不會進行型別轉換。SetField 方法會公開 (Expose) DataRow 類別的 ADO.NET 2.0 行為。DataRow 物件可以執行型別轉換,然後再將轉換的值儲存至 DataRow 物件。

請參閱

參考

DataRowExtensions