Foreach with a select/from in square brackets?
I was recently looking at wrapper classes and googled the following page...http://wiki.developerforce.com/page/Wrapper_Class
While I understood wrapper classes, I was baffled by the following...
public List<cContact> getContacts() {
if(contactList == null) {
contactList = new List<cContact>();
for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
// As each contact is processed we create a new cContact object and add it to the contactList
contactList.add(new cContact(c));
}
}
return contactList;
}
and in particular...
for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) { ... }
What is that select and from? Where can I look at more info for this in the foreach?
I know about LINQ and the select, from, where, etc.... but I never seen _this_ syntax before. What is it and how do I research more about this syntax?
I don't like leaving questions unanswered...
For the particular question raised ... http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm
For Salesforce Object Query Language (SOQL) in general - which the select/from in square brackets is know as ... http://www.salesforce.com/us/developer/docs/soql_sosl/salesforce_soql_sosl.pdf
For the APEX Language in general, since that is the language that happens to look very C#-ish (more examples of question raised)... http://wiki.developerforce.com/page/Apex_Code:_The_World's_First_On-Demand_Programming_Language
There is also this Cheat-sheet that shows more about that type of syntax, along with other ways of using APEX. Check it out here
The For loop..."defines a loop. The three types of for loops are: iteration using a variable, iteration over a list, and iteration over a query.
Example:
String s = 'Acme';
for (Account a : [SELECT Id, Name, FROM account WHERE Name LIKE :(s+'%')])
{
// Your code
}
That syntax is for the above mentioned SOQL, and it is slightly different from C#'s own layout of the LINQ syntax, although they are very similar!
链接地址: http://www.djcxy.com/p/15430.html