如何通过名称而不是整数访问属性?
问题描述:
我必须缺少一些东西,但我似乎无法弄清楚如何通过名称/字符串获取属性,只能通过一个可能会更改的属性(属性名称不是)。如何通过名称而不是整数访问属性?
你能解释我如何通过名称/字符串获取属性吗?字符串“主动”尝试会产生此错误:
Error 82 The best overloaded method match for 'System.Collections.Generic.List<Amazon.SimpleDB.Model.Attribute>.this[int]' has some invalid argumens
谢谢!
Hairgami
using (sdb = AWSClientFactory.CreateAmazonSimpleDBClient(accessKeyID, secretAccessKeyID))
{
String selectExpression = string.Format("select * from apps where appid = '{0}'", appID);
SelectRequest selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
SelectResponse selectResponse = sdb.Select(selectRequestAction);
if (selectResponse.IsSetSelectResult())
{
SelectResult selectResult = selectResponse.SelectResult;
foreach (Item item in selectResult.Item)
{
//Works fine
Amazon.SimpleDB.Model.Attribute id = item.Attribute[1];
//How can I do this:
Amazon.SimpleDB.Model.Attribute id = item.Attribute["active"];
}
}
else
{
}
}
答
你试图访问一个System.Collections.Generic.List<T>
就像是一个关联数组,它不是。 Enumerable.FirstOrDefault
方法可用于实现类似的东西:
Amazon.SimpleDB.Model.Attribute id =
item.Attribute.FirstOrDefault(attr => attr.Name == "active");
谢谢甜甜圈!我没有想过使用LINQ查询或你在做什么。这是完美的。你把所有没有拴在地上的东西摇摆起来。喔,呼! –