Share via


轉換資料型別

轉換方法會變更輸入物件的型別。

LINQ 查詢中的轉換作業適用於各種應用程式。 下列是一些範例:

方法

下表會列出執行資料型別轉換的標準查詢運算子方法。

這張表格中名稱以 "As" 開頭的轉換方法會變更來源集合的靜態 (Static) 型別,但不會列舉它。 名稱以 "To" 開頭的方法會列舉來源集合,並將項目放入對應的集合型別。

方法名稱

描述

C# 查詢運算式語法

Visual Basic 查詢運算式語法

詳細資訊

AsEnumerable

傳回型別為 IEnumerable<T> 的輸入。

不適用。

不適用。

Enumerable.AsEnumerable<TSource>

AsQueryable

將 (泛型) IEnumerable 轉換成 (泛型) IQueryable

不適用。

不適用。

Queryable.AsQueryable

Cast

將集合的項目轉換 (Cast) 為指定的型別。

使用明確指定型別的範圍變數。 例如:

from string str in words

From … As …

Enumerable.Cast<TResult>

Queryable.Cast<TResult>

OfType

根據是否可以轉換為指定的型別來篩選值。

不適用。

不適用。

Enumerable.OfType<TResult>

Queryable.OfType<TResult>

ToArray

將集合轉換為陣列。 這個方法會強制執行查詢。

不適用。

不適用。

Enumerable.ToArray<TSource>

ToDictionary

根據索引鍵選取器函式,將項目放入 Dictionary<TKey, TValue>。 這個方法會強制執行查詢。

不適用。

不適用。

Enumerable.ToDictionary

ToList

將集合轉換為 List<T>。 這個方法會強制執行查詢。

不適用。

不適用。

Enumerable.ToList<TSource>

ToLookup

根據索引鍵選取器函式,將項目放入 Lookup<TKey, TElement> (一對多字典)。 這個方法會強制執行查詢。

不適用。

不適用。

Enumerable.ToLookup

查詢運算式語法範例

下列程式碼範例會先使用明確指定型別的範圍變數 (在 C# 中) 或 From As 子句 (在 Visual Basic 中),將型別轉換為子型別,再存取只有子型別才有的成員。

Class Plant
    Public Property Name As String
End Class

Class CarnivorousPlant
    Inherits Plant
    Public Property TrapType As String
End Class

Sub Cast()

    Dim plants() As Plant = { 
        New CarnivorousPlant With {.Name = "Venus Fly Trap", .TrapType = "Snap Trap"}, 
        New CarnivorousPlant With {.Name = "Pitcher Plant", .TrapType = "Pitfall Trap"}, 
        New CarnivorousPlant With {.Name = "Sundew", .TrapType = "Flypaper Trap"}, 
        New CarnivorousPlant With {.Name = "Waterwheel Plant", .TrapType = "Snap Trap"}}

    Dim query = From plant As CarnivorousPlant In plants 
                Where plant.TrapType = "Snap Trap" 
                Select plant

    Dim sb As New System.Text.StringBuilder()
    For Each plant In query
        sb.AppendLine(plant.Name)
    Next

    ' Display the results.
    MsgBox(sb.ToString())

    ' This code produces the following output:

    ' Venus Fly Trap
    ' Waterwheel Plant

End Sub
class Plant
{
    public string Name { get; set; }
}

class CarnivorousPlant : Plant
{
    public string TrapType { get; set; }
}

static void Cast()
{
    Plant[] plants = new Plant[] {
        new CarnivorousPlant { Name = "Venus Fly Trap", TrapType = "Snap Trap" },
        new CarnivorousPlant { Name = "Pitcher Plant", TrapType = "Pitfall Trap" },
        new CarnivorousPlant { Name = "Sundew", TrapType = "Flypaper Trap" },
        new CarnivorousPlant { Name = "Waterwheel Plant", TrapType = "Snap Trap" }
    };

    var query = from CarnivorousPlant cPlant in plants
                where cPlant.TrapType == "Snap Trap"
                select cPlant;

    foreach (Plant plant in query)
        Console.WriteLine(plant.Name);

    /* This code produces the following output:

        Venus Fly Trap
        Waterwheel Plant
    */
}

請參閱

工作

HOW TO:使用 LINQ 查詢 ArrayList

參考

from 子句 (C# 參考)

From 子句 (Visual Basic)

System.Linq

概念

標準查詢運算子概觀

LINQ 查詢運算式 (C# 程式設計手冊)