Search code examples
c#amazon-simpledb

Get SimpleDB attribute without looping through all attributes for an item


I would like to check an attribute using C# and SimpleDB without having to loop through all the attributes for a specific item.

For example, if I have my domain: "MyDomain", and I have three attributes for an item (the item is: [email protected]).

The three attributes are:

Name
Signup
Contacts


So for example this is my data:

MyDomain (domain)
  [email protected] (item)
    George Doe (attribute)
    3-1-12 (attribute)
    [email protected] (attribute)



The problem I am having is my code will loop through all 3 attributes in the [email protected] item.
For example; when I run my code the output is:

No Contacts Found
No Contacts Found
Contact: [email protected]

because my code is looping through all 3 attributes for the [email protected] item.

I want to make my C# code only check the "Contacts" attribute and NOT loop through all the attributes for the [email protected] item.

Can someone please show me how to do this?

AmazonSimpleDB sdb = AWSClientFactory.CreateAmazonSimpleDBClient();

            String selectExpression = "Select * From MyDomain Where Email = '[email protected]'";
            SelectRequest selectRequestAction =
                new SelectRequest().WithSelectExpression(selectExpression);
            SelectResponse selectResponse = sdb.Select(selectRequestAction);

            if (selectResponse.IsSetSelectResult())
            {
                SelectResult selectResult = selectResponse.SelectResult;
                foreach (Item item in selectResult.Item)
                {

                    foreach (Amazon.SimpleDB.Model.Attribute attribute in item.Attribute)
                    {    
                        if (attribute.IsSetName())
                        {

                            if (attribute.Name == "Contact")
                            {
                                if (attribute.IsSetValue())
                                {
                                    Console.WriteLine("Contact: {0}", attribute.Value);
                                }
                            }
                            else
                            {
                                Console.WriteLine("No Contacts Found");
                            }   
                        }
                    }
                }
            }


I want it to directly just check the "Contacts" attribute to see if there is a value for it and not loop through all the attributes.



Can someone show me a C# code example on how I would accomplish this? Thanks.


EDIT: I still want to loop through all the "Contacts" in the "Contacts" attribute. I just don't want to loop through all the attributes that don't have the name "Contacts". The "Contacts" attribute can hold more than one contact and I still need to get all of those from the attribute.

This line of code from Ken below:

string value = item.Attribute.First(a => a.Name == "Contact").Value

stops the looping of the different attributes, but it only returns 1 contact from the "Contacts" attribute even though there are more than 1 contact stored in the "Contacts" attribute.


Solution

  • You can run Amazon simpleDB Query with Attribute.

    select <attribute_name> from <domain_name>
    

    And Amazon SimpleDB will return only items that have this attribute and in response you will get only this attribute even items have other attributes.