Get the first and second objects from a list using LINQ

Posted by Vahid on Stack Overflow See other posts from Stack Overflow or by Vahid
Published on 2014-06-05T09:21:13Z Indexed on 2014/06/05 9:25 UTC
Read the original article Hit count: 295

Filed under:
|
|

I have a list of Person objects. How can I get the first and second Person objects that meet a certain criteria from List<Person> People using LINQ?

Let's say here is the list I've got. How can I get the first and second persons that are over 18 that is James and Jodie.

public class Person
{
    public string Name;
    public int age;
}

var People = new List<Person>
{
   new Person {Name = "Jack", Age = 15},
   new Person {Name = "James" , Age = 19},
   new Person {Name = "John" , Age = 14},
   new Person {Name = "Jodie" , Age = 21},
   new Person {Name = "Jessie" , Age = 19}
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ