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: george@george.com).
The three attributes are:
Name
Signup
Contacts
So for example this is my data:
MyDomain (domain)
george@george.com (item)
George Doe (attribute)
3-1-12 (attribute)
mike@mike.com (attribute)
The problem I am having is my code will loop through all 3 attributes in the george@george.com item.
For example; when I run my code the output is:
No Contacts Found
No Contacts Found
Contact: mike@mike.com
because my code is looping through all 3 attributes for the george@george.com item.
I want to make my C# code only check the “Contacts” attribute and NOT loop through all the attributes for the george@george.com item.
Can someone please show me how to do this?
AmazonSimpleDB sdb = AWSClientFactory.CreateAmazonSimpleDBClient();
String selectExpression = "Select * From MyDomain Where Email = 'george@george.com'";
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.
You can run Amazon simpleDB Query with Attribute.
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.