【发布时间】:2013-03-26 01:56:42
【问题描述】:
我找到了以下过滤列表的扩展方法。我对此很陌生,因此我想检查是否有人可以帮助我。此方法比较精确值,但我想使用包含而不是精确比较。有什么想法
public static IEnumerable<T> FilterByProperty<T>(this IEnumerable<T> source,string property,object value)
{
var propertyInfo = typeof(T).GetProperty(property);
return source.Where(p => propertyInfo.GetValue(p, null) == value);
}
【问题讨论】:
-
为什么不直接使用
Where子句?list.Where(c => c.MyProperty == "someValue"); -
MyProperty 已修复,我想让它动态化..
-
我认为你不能假设
Contains是为Object定义的 :)
标签: linq c#-4.0 extension-methods