Nic*_*ckP 0 vb.net asp.net-mvc-3
我有一个LINQ查询如下:
Dim CustQuery = From a In db.Customers
Where a.GroupId = sendmessage.GroupId
Select a.CustCellphone
Run Code Online (Sandbox Code Playgroud)
并希望通过每个结果并获得手机号码来做一些代码.我尝试了以下但似乎无法使其正确:
For Each CustQuery.ToString()
...
Next
Run Code Online (Sandbox Code Playgroud)
那么我的问题是我该怎么做?
您必须在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
Run Code Online (Sandbox Code Playgroud)
现在,如果您的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
Run Code Online (Sandbox Code Playgroud)