Foreach using select / from in square brackets?

I recently looked at wrapper classes and looked at the following page ... http://wiki.developerforce.com/page/Wrapper_Class

While I understood wrapper classes, I was puzzled 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 it is? Where can I see more information about this in foreach?

I know about LINQ and select, from, where, etc., but I have never seen the syntax of _this _ . What is it and how do I understand this syntax more?

+7
c # foreach apex-code
source share
2 answers

I do not like to leave questions unanswered ...

For the specific question raised ... http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm

For Salesforce Object Query Language (SOQL) as a whole - which in square brackets select / from is known as ... http://www.salesforce.com/us/developer/docs/soql_sosl/salesforce_soql_sosl.pdf

For APEX in general, since it is a language that looks very similar to C # -ish (more examples of the question raised) ... http://wiki.developerforce.com/page/Apex_Code:_The_World's_First_On-Demand_Programming_Language

+2
source share

There is also this cheat sheet that describes this type of syntax more, as well as other ways to use APEX. Check here

The For ... loop defines a loop. Three types of loops: iteration using a variable, iteration through a list, and iteration upon request.

Example:

 String s = 'Acme'; for (Account a : [SELECT Id, Name, FROM account WHERE Name LIKE :(s+'%')]) { // Your code } 

This syntax is for the aforementioned SOQL, and it is slightly different from the native C # layout of LINQ syntax, although they are very similar!

+1
source share

All Articles