當我們遍歷一個已知實體類時我們可以這樣來做,但是動態實體無法獲取到類的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())) { } }