循环查看Linq查询

时间:2012-08-22 13:21:08

标签: vb.net asp.net-mvc-3

我有一个LINQ查询,如下所示:

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

并希望仔细检查每个结果并获取手机号码以进行操作。我尝试过以下但似乎无法正确理解:

For Each CustQuery.ToString()
   ...
Next

那么我的问题是我该怎么做?

1 个答案:

答案 0 :(得分: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