List Operation's Select

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.