[英]Access an attribute from a returned object in c#
Good Day. 美好的一天。 I am struggling with what I would assume would be a novice issue, I have an object being returned from a method in my data layer, but I want to be able to access say
我正在努力解决我认为是新手的问题,我在数据层的方法中返回了一个对象,但我希望能够访问说
RuleListCollection.ruleName RuleListCollection.ruleName
but I have the whole object sent back to me in the list, how can i access the attribute above. 但是我在列表中将整个对象发送回了我,我该如何访问上面的属性。 here is the code from the data layer where the object is returned
这是返回对象的数据层中的代码
public List<TpRuleMapConfig> GetTpRuleList()
{
StringBuilder query = new StringBuilder();
TpRuleMapConfig tpRuleMapConfig = new TpRuleMapConfig();
List<TpRuleMapConfig> tpRuleList = new List<TpRuleMapConfig>();
try
{
query.AppendLine("SELECT maestro.TPRuleMapConfig.LINK,maestro.TPRuleMapConfig.TPRULENAME,maestro.TPRuleMapConfig.SBSARULENAME,");
query.AppendLine(" maestro.COUNTINGCONFIG.RULETYPE,maestro.TPRuleMapConfig.ISPREADVRULE FROM maestro.TPRuleMapConfig");
query.AppendLine("INNER JOIN maestro.COUNTINGCONFIG on maestro.COUNTINGCONFIG.NAME=TPRuleMapConfig.SBSARULENAME;");
using (SqlConnection con = new SqlConnection(this._connectionString))
{
using (SqlCommand cmd = new SqlCommand(query.ToString()))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
tpRuleMapConfig.Link = reader["LINK"].ToString();
tpRuleMapConfig.TpRuleName = reader["TPRULENAME"].ToString();
tpRuleMapConfig.SbsaRuleName = reader["SBSARULENAME"].ToString();
tpRuleMapConfig.RuleType = Convert.ToInt32(reader["RULETYPE"]);
tpRuleMapConfig.IsPreadVised = reader["ISPREADVRULE"].GetBooleanValue();
tpRuleList.Add((tpRuleMapConfig));
}
}
}
}
}
catch (Exception)
{
throw;
}
return tpRuleList;
}
and here is where I want to access that value 这是我要获取该价值的地方
DataLayer datalayer = new DataLayer(this._connectionString);
cmbRuleType.DataSource = datalayer.GetTpRuleList().TpRuleName;
Your method returns a List
of objects not a single object. 您的方法返回一个对象
List
,而不是单个对象。 You can iterate over the list and read the RuleName
of each object in the list. 您可以遍历列表并读取列表中每个对象的
RuleName
。
var tpRuleList = datalayer.GetTpRuleList();
foreach (var tpRule in tpRuleList)
{
var tpRuleName = tpRule.TpRuleName;
// Do something with tpRuleName.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.