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"
var results = listOfPerson.Where(
p => p.Name.StartsWith("pra", StringComparison.CurrentCultureIgnoreCase));
foreach(Person p in results)
{
...
}
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)
{
...
}
精彩评论