+ Start a Discussion

Relationship Query sample in .NET

is there a sample using a relationship query in .net you can point me to?  I have looked all through the forums but can’t find anything for .net… I have something like this from the samples…


private string getFieldValue(string fieldName, System.Xml.XmlElement[] fields) {
    string returnValue = "";
    if (fields != null) {
        for (int i = 0; i < fields.Length; i++) {
            if (fields[i].LocalName.ToLower().Equals(fieldName.ToLower())) {
                returnValue = fields[i].InnerText;
    return returnValue;

My function…
string q = "Select a.Id,a.Name,a.Field1__c,a.Field2__c," +
    "(Select d.Id,d.Name,d.Related_Field1__c,d.Related_Field2__c From MyRelatedObject__r d) " +
    "From MyCustomObject__c a";

QueryResult qr = m_binding.query(q);
sObject sobj = null;

for (int i=0; i<qr.records.Length; i++) {
    sObject record = qr.records[i];

    string objId = getFieldValue("Id", record.Any);
    string objName = getFieldValue("Name", record.Any);
    string objField1 = getFieldValue("Field1__c ", record.Any);
    string objField2 = getFieldValue("Field2__c ", record.Any);

    //how do I access the related object query results—
    //can I use QueryResult again or do I have to use xml methods to get at the sub results–
    //I want to get at the fields below…
    string relatedObjId = getFieldValue("Id", subrecord.Any);
    string relatedObjName = getFieldValue("Name", subrecord.Any);
    string relatedObjField1 = getFieldValue("Related_Field1__c", subrecord.Any);
    string relatedObjField2 = getFieldValue("Related_Field2__c", subrecord.Any);


Thanks for any help you can provide as I am stuck…

What are you stuck doing?  Executing the query or processing the results?
processing the results.  how can i get to the relationship query results?
The results will be an embedded QueryResults object.

If the relationship name is Contacts, then the Account.Contacts object will be a query result that you would process like the outer QueryResult.