日期:2014-05-18  浏览次数:20810 次

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