
IEnumerable<dynamic> List = _repository.All(whereClause);
然后,我需要对该列表执行的 *** 作是获取数组中指定的每个列名的唯一值列表.所以像这样:
List<string> columns = new string[] {"Col1","Col1"};foreach(string column in columns){ //get unique value for column and add them to some collection List.Select().Where(???)} 因为列表是动态的,所以我不确定如何根据列名进行选择.
任何人都可以帮忙
解决方法 如何使用辅助类按名称动态访问属性(可以扩展为使用一些缓存):public class ObjectUtils{ public static object GetPropertyByname(object obj,string name) { if (obj == null) { return null; } PropertyInfo propInfo = obj.GetType().GetProperty(name); if (propInfo == null) { return null; } object value = propInfo.GetValue(obj,null); return value; }} 然后得到这样的数据:
List<dynamic> List = new List<dynamic>();List.Add(new { Col1 = "AB",Col2 = 23 });List.Add(new { Col1 = "CD",Col2 = 23 });List.Add(new { Col1 = "AB",Col2 = 5 });List.Add(new { Col1 = "EF",Col2 = 9 });string[] columns = new string[] { "Col1","Col2" };foreach (string column in columns){ var elems = List.Select(d => ObjectUtils.GetPropertyByname(d,column)).distinct().ToList(); // elems will be "AB","CD","EF" for Col1 // and 23,5,9 for Col2} 如果它不编译,请确保添加对Microsoft.CSharp的引用
总结以上是内存溢出为你收集整理的c# – 从动态列表中获取唯一的值列表全部内容,希望文章能够帮你解决c# – 从动态列表中获取唯一的值列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)