简体   繁体   English

如果它们与数据表中的列名匹配,则选择T的属性,并忽略大小写

[英]select properties of T if they match a column name in a datatable and ignore case

i have a part of a generic function where i need to copy property info to a list only if its name matches one of the datatable columns. 我有一个通用函数的一部分,仅当名称与datatable列之一匹配时,才需要将属性信息复制到列表中。

    DataTable dt = new DataTable();
    .
    .
    .
//here i want only the properties that have the same name of some column in the datatable
    List<PropertyInfo> properties = typeof(T).GetProperties().ToList();

how can i do this using LINQ? 我如何使用LINQ做到这一点?

i tried 我试过了

List<PropertyInfo> properties = typeof(T).GetProperties().Where(p=> dt.Columns.Contains(p.Name)).ToList();

but it doesnt seem to work that way, because i need to ignore case on both sides property name and column name 但这似乎不起作用,因为我需要忽略属性名称和列名称的大小写

problem with ToUpper() and ToLower() functions is some times the column names are not totally lower case nor upper case ToUpper()和ToLower()函数的问题是某些时候列名不是完全小写也不是大写

尝试这样的事情:

List<PropertyInfo> properties = typeof(T).GetProperties().Where(p => dt.Columns.Cast<DataColumn>().Any(c => c.ColumnName.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase))).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM