0

次のようなLINQクエリがあります。

Dim CustQuery = From a In db.Customers
                Where a.GroupId = sendmessage.GroupId
                Select a.CustCellphone

そして、各結果を調べて、携帯電話番号を取得してコードを実行したいと思います。私は次のことを試しましたが、正しく取得できないようです:

For Each CustQuery.ToString()
   ...
Next

だから私の質問は、どうすればこれを行うことができますか?

4

1 に答える 1

5

ループ内で使用するために、コレクション内の各項目の値を格納する変数を For Each ループに設定する必要があります。VB For Each ループの正しい構文は次のとおりです。

For Each phoneNumber In CustQuery
    //each pass through the loop, phoneNumber will contain the next item in the CustQuery 
    Response.Write(phoneNumber)     
Next

LINQ クエリが複雑なオブジェクトである場合は、次の方法でループを使用できます。

Dim CustQuery = From a In db.Customers
                Where a.GroupId = sendmessage.GroupId
                Select a

For Each customer In CustQuery
    //each pass through the loop, customer will contain the next item in the CustQuery 
    Response.Write(customer.phoneNumber)     
Next
于 2012-08-22T13:26:15.877 に答える