Search code examples
c#foreachapex-code

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?


Solution

  • 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