. NET 3. x Era
Automatic attributes, extension methods, implicit types, anonymous types, type sets, and lambda expressions greatly simplify programming complexity.
Define object class:
Use automatic attributes.
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
}
Initialization set:
list = new List<Employee>
{
new Employee{ Name = "Zxjay", Age = 20, Phone = "010-123456" },
new Employee{ Name = "Andy", Age = 30, Phone = "020-123456" },
new Employee{ Name = "Bill", Age = 50, Phone = "010-345678" },
new Employee{ Name = "Lee", Age = 40, Phone = "010-234567" }
};
Sort selection:
Implemented Using lambda expressions:
list.Sort((Employee x, Employee y) => { return x.Name.CompareTo(y.Name); });
var listBijing = list.FindAll(
(Employee emp) => { return emp.Phone.StartsWith("010") && emp.Age < 50; });
Output set elements:
Use the extension method.
private void ShowList(List<Employee> list)
{
Console.WriteLine("{0,-20:G}{1,-5:G}{2}", "Name", "Age", "Phone");
list.ForEach((Employee emp) => Console.WriteLine("{0,-20:G}{1,-5:G}{2}", emp.Name, emp.Age, emp.Phone));
}
These queries are much simpler, but these are still object-oriented methods.
In this way, the sorting and selection operations are implemented by using LINQ:
var selectResult = from emp in list
where emp.Phone.StartsWith("010") && emp.Age < 50
orderby emp.Name
select emp;
This is the real language integration query, isn't it familiar? Similar to SQL statements, the SELECT statement is placed at the end.
This article is just a brief introduction to the overview of LINQ and the changes in the collection query mechanism in languages. The content of LINQ will be further expanded in the following content.
Original article: http://tech.ddvip.com/2008-12/122872526398411_3.html