C#获取dynamic(动态)实体的属性值
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())) { } }
dynamic集合动态添加属性
List<dynamic> list = conn.Query(srhsql, p).ToList(); //查出数据 List<dynamic> tmp = new List<dynamic>(); foreach (var row in list) { dynamic info = new System.Dynamic.ExpandoObject(); var dic = (IDictionary<string, object>)info; foreach (KeyValuePair<string, object> col in row) { dic.Add(col); } dic.Add("名称", "值"); tmp.Add(info); } list = tmp;}
原文链接:
https://www.cnblogs.com/leebokeyuan/p/14128860.html
https://blog.csdn.net/qq_39569480/article/details/104480012
标签:string,List,dynamic,添加,foreach,col,属性 From: https://www.cnblogs.com/lhlong/p/16968521.html