Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a list of objects with a certain attribute

Tags:

c#

class Object
{
    public int ID {get; set;}
    public string description {get; set;}
}

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"
like image 222
proseidon Avatar asked Mar 06 '13 17:03

proseidon


People also ask

How do you filter an object array based on attributes?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do you filter a list of objects in Python?

Python has a built-in function called filter() that allows you to filter a list (or a tuple) in a more beautiful way. The filter() function iterates over the elements of the list and applies the fn() function to each element. It returns an iterator for the elements where the fn() returns True .

How to filter object in JavaScript?

keys() to filter an Object. After you've generated the keys, you may use filter() to loop over the existing values and return just those that meet the specified criteria. Finally, you can use reduce() to collect the filtered keys and their values into a new object, for instance.


1 Answers

You can use LINQ:

var results = Objects.Where(o => o.Description == "test");

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#.

like image 147
Reed Copsey Avatar answered Oct 04 '22 14:10

Reed Copsey