PramodChoudhari
PramodChoudhari

Reputation: 2553

Get list of object with matching property

public class Person
{
    public string Name { get; set; }
}

List<Person> listOfPerson=new List<Person>();
listOfPerson.Add(new Person(){Name="Pramod"});
listOfPerson.Add(new Person(){Name="Prashant"});
listOfPerson.Add(new Person(){Name="Sachin"});
listOfPerson.Add(new Person(){Name="Yuvraj"});
listOfPerson.Add(new Person(){Name="Virat"});

I want a LINQ Solution which will return list of object whose Name property starts with "pra"

Upvotes: 1

Views: 2690

Answers (2)

BenCr
BenCr

Reputation: 6052

Thomas's solution uses the LINQ extension methods, this uses the full LINQ query syntax.

var query = from x in listOfPerson 
            where x.Name.StartsWith("pra")
            select x;

foreach(var p in query)
{
    ...
}

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292425

var results = listOfPerson.Where(
    p => p.Name.StartsWith("pra", StringComparison.CurrentCultureIgnoreCase));

foreach(Person p in results)
{
    ...
}

Upvotes: 9

Related Questions