This article is for the new C # Novice, the veteran will not see, speaking is actually LINQ, thank the first floor of the reminder.
A lot of times, it's easy to pick a list of elements we need from a relational table, and it's easier to use SQL statements, but you can also use a similar approach in the list of C #, although the list incorporates select (), where () statements, but if your rules are more complex, or want to look at a glance, the following methods are also feasible:
First, suppose you have a class
public class people{Public string Name {get; set;} public int Age {get; set;}}
And there are some initialization statements
list<people> peoplelist = new list<people> (); Peoplelist.add (New People () {Name = "Haocheng Wu", age = 24}); Peoplelist.add (New People () {Name = "Haocheng Wu", age = 25}); Peoplelist.add (New People () {Name = "James Wu", age = 23});
You can use the following method, similar to SQL statements, to select
List<string> SubPeopleNameList1 = (from people in Peoplelist where people. Name = = "Haocheng Wu" && people. Age = = Select people. Name). Tolist<string> ();
of course, you can use one line instead .
list<string> SubPeopleNameList2 = peoplelist.where (people = people. Name = = "Haocheng Wu" && people. Age = = 24). Select (people = people. Name). ToList ();
But obviously the first method is more obvious, especially when judging conditions are quite complicated.
List Operation's Select