如果我的字段都在一个集合里。string fields[] = { "col1", "col2" };
我如何才能找出这些字段作为查询条件呢?DataTable dt = new DataTable();
...dt.Where (t => t.col1.contains("11") || t.col2.contains("11") );似乎是动态的predicate的问题。

解决方案 »

  1.   

    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);  
    }  
      

  2.   

    http://blog.csdn.net/q107770540/article/details/6133484
      

  3.   

    用反射,GetPropertyValue可以实现你要的效果,督察已经给了
      

  4.   


    感谢,本来想硬搬你处理Pets的写法的,但是发现除此之外,还要处理每个列,所以不得不把问题详细提出来了。
    其实我想实现的是在"所有可见列"中查询的效果。grid.DataSource = dt;
    Func<T, bool> predicate = null;
    foreach (var col in grid.Columns)
    {
        if (col.Visible)
            //predicate = t => GetPropertyValue(t, col.FieldName).ToString().Contains(str);
    }
    grid.DataSource = dt.Where(predicate).ToList();现在只剩下不知道怎么在每次loop时增量查询条件了。
                    
      

  5.   

    已解决,加上这个。剩下的只是重绘的栏无法查到了。http://www.cnblogs.com/atison/articles/1325915.html