开发者

Get list of object with matching property

开发者 https://www.devze.com 2023-02-22 07:04 出处:网络
public class Person { public string Name { get; set; } } List<Person> listOfPerson=new List<Person>();
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)
{
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消