通用方法来过滤列表对象 [英] Generic method to filter a list object

查看:187
本文介绍了通用方法来过滤列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个带有三个参数的泛型方法。
1)列表集合
2)字符串属性名称
3)字符串FilterString



这个想法是我们传递一个对象集合对象
的属性名称和一个过滤条件,它返回一个对象列表,其中属性包含
FilterString。

,PropertyName是可选的,所以如果它没有提供,我想返回任何属性中包含FilterString的所有对象


任何指针都是

我想要一个像这样的方法签名:
public static List FilterList(String FilterString,String Property =)

这样我就可以从任何地方调用这个方法,并将它传递给任何List,它会返回一个过滤列表。

解决方案

您可以使用LINQ来做你想做的事情,例如

  var collection = ... 
var filteredCollection =
collection.Where(item => item.Property ==something)。ToList();

否则,您可以尝试反射,

  public List< T> Filter< T>(
List< T> collection,
string property,
string filterValue)
{
var filteredCollection = new List< T>
foreach(集合中的var项)
{
//检查多个属性的使用,
// item.GetType()。GetProperties(BindingFlags.Public | BindingFlags.Instance)

var propertyInfo =
item.GetType()
.GetProperty(property,BindingFlags.Public | BindingFlags.Instance);
if(propertyInfo == null)
throw new NotSupportedException(给定的属性不存在);

var propertyValue = propertyInfo.GetValue(item,null);
if(propertyValue == filterValue)
filteredCollection.Add(item);
}

return filteredCollection;





$ b

这个解决方案的问题在于属性名称或拼写错误导致一个运行时错误,而不是一个编译错误,因为使用名称是硬类型的实际属性表达式。另外,请注意,基于绑定标志,这只会在 public 非静态属性上起作用。您可以通过传递不同的标志


I am trying to create a generic method that takes three parameters. 1) List collection 2) String PropertyName 3) String FilterString

The idea is we pass a collection of Objects, the name of the property of the object and a Filter Criteria and it returns back a list of Objects where the property contains the FilterString.

Also, the PropertyName is optional so if its not supplied I would like to return all objects that contain the FilterString in any property.

Any pointers on this would be really helpful.

I am trying to have a method signature like this: public static List FilterList(List collection, String FilterString, String Property = "")

This way I can call this method from anywhere and pass it any List and it would return me a filtered list.

解决方案

You could do what you want using LINQ, as such,

var collection = ...
var filteredCollection = 
    collection.Where(item => item.Property == "something").ToList();

Otherwise, you could try Reflection,

public List<T> Filter<T>(
    List<T> collection, 
    string property, 
    string filterValue)
{
    var filteredCollection = new List<T>();
    foreach (var item in collection)
    {
         // To check multiple properties use,
         // item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)

         var propertyInfo = 
             item.GetType()
                 .GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
         if (propertyInfo == null)
             throw new NotSupportedException("property given does not exists");             

         var propertyValue = propertyInfo.GetValue(item, null);
         if (propertyValue == filterValue)
             filteredCollection.Add(item);       
    }

    return filteredCollection;
}

The problem with this solution is that changes to the name of the property or misspellings result in a runtime error, rather than a compilation error as would be using an actual property expression where the name is hard-typed.

Also, do note that based on the binding flags, this will work only on public, non-static properties. You can modify such behavior by passing different flags.

这篇关于通用方法来过滤列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆