NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How to find an item in a collection on a known index?

I want to find an item in a collection on a fix index. i can remove an item like this

  my_Order.OrderItems.RemoveAt(id - 1);

But i want to know this item in a variable before deleting how can i find this?

  FoodItem fd= my_Order.OrderItems.

Upvotes: 0

Views: 57

Answers (2)

User 12345678
User 12345678

Reputation: 7804

If the collection exposes an indexer, you could use it:

 var element = my_Order.OrderItems[id -1]

If the collection does not expose an indexer you could use Enumerable.ElementAt():

 var element = my_Order.ElementAt(id -1)

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

FoodItem fd = my_Order.OrderItems[id - 1];

Upvotes: 1

Related Questions