c# - Pulling objects from a collection that satisfies a condition -
c# - Pulling objects from a collection that satisfies a condition -
i wondering whether know using linq in c# pull list of objects collection satisfies condition?
i trying pull list of person objects list ids match in integer list. please see code below (i trying pull person objects ids in integer list).
public class person { public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } } ... var intlist = new list<int>() { 1, 2, 3, 4 }; var perlist = new list<person> { new person {id = 1, firstname = "thomas", lastname = "joseph"}, new person {id = 2, firstname = "joseph", lastname = "austin"}, new person {id = 3, firstname = "lee", lastname = "hentry"}, new person {id = 4, firstname = "abraham", lastname = "tony"} };
you can utilize where , contains methods:
var people = perlist .where(person => intlist.contains(person.id)) .tolist(); c# linq extension-methods
Comments
Post a Comment