过滤具有特定属性的对象列表 [英] Filtering a list of objects with a certain attribute
本文介绍了过滤具有特定属性的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Object
{
public int ID {get; set;}
public string description {get; set;}
}
如果我有 List< Object>对象
中填充了各种对象,我想找到描述特别的对象,我该怎么做?
If I have a List<Object> Objects
populated with various objects, and I want to find objects whose description is something particular, how would I do that?
find every Object in Objects whose description == "test"
推荐答案
您可以使用LINQ:
var results = Objects.Where(o => o.Description == "test");
另一方面,请注意, Object
对于类的名称来说是一个很差的选择,甚至都不能按原样编译...我建议选择更合适的名称,并遵循C#的标准大写约定.
On a side note, realize that Object
is a very poor choice of names for a class, and won't even compile as-is... I'd recommend choosing more appropriate names, and following standard capitalization conventions for C#.
这篇关于过滤具有特定属性的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文