0

我发现了以下过滤列表的扩展方法。我对此很陌生,因此我想检查是否有人可以帮助我。此方法比较精确值,但我想使用包含而不是精确比较。有什么想法吗

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

1 回答 1

0

如果要将等号更改==Contains. 尝试这个:

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)
                                      .ToString() //be aware that this might be null
                                      .Contains(value));
    }
于 2013-04-04T06:29:10.743 回答