LINQ: start to use LINQ (3)-use LINQ for data conversion, and use linq for Data Conversion

Source: Internet
Author: User

LINQ: start to use LINQ (3)-use LINQ for data conversion, and use linq for Data Conversion
Getting started with LINQ (3)-using LINQ for Data Conversion


Language integration query (LINQ) is not only used to retrieve data, but also a powerful data conversion tool. By using LINQ query, you can use the source sequence as the input and modify it in multiple ways to create a new output sequence. You can modify the sequence by sorting and grouping without modifying the element itself. However, the most powerful function of LINQ query is to create new types. This function is implemented in the select clause. For example, you can execute the following tasks:

 

1 class Student 2 {3 public string Name {get; set;} 4 5 public int Age {get; set;} 6 7 public string City {get; set ;} 8 9 public List <int> Scores {get; set;} 10} 11 12 class Teacher13 {14 public int Id {get; set;} 15 16 public string Name {get; set;} 17 18 public int Age {get; set;} 19 20 public string City {get; set;} 21 22}Students and teachers

1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 // create the first data source 6 var students = new List <Student> () 7 {8 new Student () 9 {10 Age = City = "Guangzhou", 12 Name = "small C", 13 Scores = new List <int> () {, 88, 83,97} 14}, 15 new Student () 16 {17 Age = 18, 18 City = "Guangxi", 19 Name = "James", 20 Scores = new List <int> () {,} 21}, 22 new Student () 23 {24 Age = City = "dream", 26 Name = "xiao san ", 27 Scores = new List <int> () {86,68, 73,97} 28} 29}; 30 31 // create the second data source 32 var teachers = new List <Teacher> () 33 {34 new Teacher () 35 {36 Age = 35, 37 City = "dream", 38 Name = "too many" 39}, 40 new Teacher () 41 {42 Age = 28, 43 City = "Yunnan", 44 Name = "Xiaohong" 45}, 46 new Teacher () 47 {48 Age = 38,49 City = "Henan ", 50 Name = "Lili" 51} 52}; 53 54 // create a query 55 var peopleInDreams = (from student in students56 where student. city = "dream" 57 select student. name) 58. concat (from teacher in teachers59 where teacher. city = "dream" 60 select teacher. name); 61 62 // execute query 63 foreach (var person in peopleInDreams) 64 {65 Console. writeLine (person); 66} 67 68 Console. read (); 69} 70}

 

2. Select a subset of each source element

1. to select only one member of the Source Element, use the vertex operation.

1 var query = from cust in Customers2             select cust.City;

 

2. To create an element that contains multiple attributes of the Source Element, you can use an object with a named object or an anonymous type.

1 var query = from cust in Customer2             select new {Name = cust.Name, City = cust.City};
3. Convert objects in memory to XML
1 // create A data source 2 var students = new List <Student> () 3 {4 new Student () 5 {6 Age = 18, 7 Name = "", 8 Scores = new List <int> () {88,85,} 9}, 10 new Student () 11 {12 Age = 35, 13 Name = "Small B ", 14 Scores = new List <int> () {88,85,} 15}, 16 new Student () 17 {18 Age = 28, 19 Name =" ", 20 Scores = new List <int> () {88,85,} 21} 22}; 23 24 // create a query 25 var studentsToXml = new XElement ("Root ", 26 from student in students27 let x = $ "{student. scores [0]}, {student. scores [1]}, {student. scores [2]}, {student. scores [3]} "28 select new XElement (" student ", 29 new XElement (" Name ", student. name), 30 new XElement ("Age", student. age), 31 new XElement ("Scores", x) 32); 33 34 // execute the query 35 Console. writeLine (studentsToXml );
4. perform operations on the Source Element

The output sequence may not contain any element or element attribute of the source sequence. The output may be a sequence of values calculated by using the source element as an input parameter.

1 // data source 2 double [] radii = {1, 2, 3 }; 3 4 // create a query 5 var query = from radius in radii 6 select $ "{radius * 3.14 }"; 7 8 // run the query 9 foreach (var I in query) 10 {11 Console. writeLine (I); 12}

 

[Source] This article mainly comes from Microsoft official documentation

 

Related Article

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.