当我们遍历一个已知实体类时我们可以这样来做,但是动态实体无法获取到类的GetType()

    List<student> item= conn.Query<student>($"select * from 表  where id=123 ").ToList();
    foreach (System.Reflection.PropertyInfo p in item.GetType().GetProperties())
                {
                    Console.WriteLine(p.Name+ " :"+p.GetValue(item, null));
                }
     
    class student{
    public string id{get;set;}
    public string name{get;set;}
    public string sex{get;set;}
    }

当我们需要遍历动态一个实体想要知道某个字段有没有值时,我们可以这样来写

            List<dynamic> result = conn.Query($"select * from 表 where id='123'").ToList();
            foreach (KeyValuePair<string, object> col in result[0])
            {
                string aa = col.Key;//属性
                string bb = col.Value.ToString();//
                if (!string.IsNullOrWhiteSpace(col.Value.ToString()))
                {

                }
            }

 

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
猜你喜欢
  • 2021-06-20
  • 2021-09-24
  • 2021-08-11
  • 2022-12-23
  • 2021-06-15
  • 2021-05-21
相关资源
相似解决方案