读书人

linq 以指命名称的列作为查询条件

发布时间: 2012-08-11 20:50:31 作者: rapoo

linq 以指定名称的列作为查询条件
如果我的字段都在一个集合里。

string fields[] = { "col1", "col2" };


我如何才能找出这些字段作为查询条件呢?

DataTable dt = new DataTable();
...

dt.Where (t => t.col1.contains("11") || t.col2.contains("11") );

似乎是动态的predicate的问题。



[解决办法]
dt.Where (t => GetPropertyValue(t,"col1").Contains("11") );

private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
[解决办法]
http://blog.csdn.net/q107770540/article/details/6133484

读书人网 >C#

热点推荐