Foreach with a select/from in square brackets?
Asked Answered
D

2

7

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?

Democritus answered 22/8, 2013 at 7:20 Comment(5)
I don't think it is C#... "In Apex and Visualforce this type of class ..." - looks like name of the language is "Apex" according to your link... (re-tagged the question)Polysemy
Yep, looks like: salesforce.com/us/developer/docs/apexcode/Content/…Shcherbakov
Yeah i gues so it is Apex There’s Apex, a language that superficially looks like C#, but requires a completely different set of design patterns.... and guess the right term for it is SOQL(Salesforce Object Query Language)Riant
Looks like java's iterator loopCurtin
Yeah, I was trying to find the language. But I seen the...{ get; set; } which I only knew about from C#, and I thought was kind of MS's.Democritus
D
2

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

Democritus answered 22/8, 2013 at 7:49 Comment(0)
I
1

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!

Ingvar answered 24/8, 2013 at 2:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.