The era of LINQ Asp.net 3.x

Source: Internet
Author: User

. 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

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.