select object which matches with my condition using linq

For one match:

var match = persons.Single(p => your condition);

For many matches, use persons.Where(condition). There are also many variants of picking just one person, such as FirstOrDefault, First, Last, LastOrDefault, and SingleOrDefault. Each has slightly different semantics depending on what exactly you want.


You can use Enumerable.Where and it will return all the matching elements collection.

var res = persons.Where(c=>c.AttributeName == 23);

If you want to ensure you have only match you can use single.

var res = persons.Single(c=>c.AttributeName == 23);

Single Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Tags:

C#

.Net

Linq