How can I search for multiple ids in an array of users
For example:
I have this array of numbers[10,22,34,55];
And I have a list of users, it is an array that has several objects:
[
{ id: 1, name: John },
{ id: 10, name: Mike },
{ id: 12, name: Dwee },
{ id: 22, name: Jesus },
{ id: 30, name: July },
{ id: 34, name: Juan },
{ id: 36, name: Lusy },
{ id: 55, name: Susy },
{ id: 59, name: Dani },
]
So how can I get the names of the following array [10,22,34,55];
, that is, how can I filter it, I was thinking of doing it with a foreach, and compare the values, but I don't know if there is a more compact way to do it, maybe apply a Where or Select
Select
creates a new list by doing a transformation to each element. If you're coming from javascript, it's very similar tomap
.You can put the ids in a list and with
Where
filter the items based on whether the person's id is in the list.It is worth mentioning that the notation you use corresponds to JSON. In C# you use braces to write a list.
Anyway, you create a structure to help you and you get a code like this: