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 回答