首页 > 解决方案 > 获取列表中对象的属性值

问题描述

我想从列表中的对象获取属性值并将其放入 textbox.text

下面我有一个我的代码示例:

物体:

public class Incident
    {
        public int Incident_id { get; set; }
        public string Description { get; set; }
        public string Caller { get; set; }
}

以下是我的表单类中的代码:

List<Incident> incidentById = new List<Incident>();

 incidentById = db.GetIncidentById(ID);

当我的列表被填满时,我想将字符串 Caller 放入一个文本框中,如下所示:

textBoxCaller.Text = incidentById[1].Caller;

我被困在这一点上,所以我希望有人可以帮助我。谢谢!

编辑:

public List<Incident> GetIncidentById(int id)
        {

            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("IncidentLog")))
            {
                var output = connection.Query<Incident> ($"select * from Incidents where Incident_id like @id", new { id = "%id%" }).ToList();

                return output;
            }
        }

标签: c#

解决方案


我没有将正确的值传递给我的查询,这成功了!你想要的是$"select * from Incidents where Incident_id = @id", new { id }


推荐阅读