Notes for learning from the basics of LinQ (1) getting started with LINQ to (Objects, XML, SQL), linqobjects
For the author, the beautiful and concentrated code is shocking. The research of LINQ is to write its own code.
Previously, I learned the basic syntax just by taking a look at it, but I often see the surprising and sighing LINQ code in CSDN, which is still enviable. There is really an impulse to catch up.
Therefore, it is decided that the system should learn about the LINQ.
(1.4) LINQ to Objects
This is a simple example of hello world.
1 var words = new List <string> () {"hello", "wonderful", "linq", "beautiful", "world "}; 2 var res = from s in words 3 where s. length <= 5 4 select s; 5 Console. writeLine ("output String Length <= 5 words:" + Environment. newLine); 6 foreach (var m in res) 7 {8 Console. writeLine (m); 9} 10 11 // sort a word in alphabetical order, group the word according to its length, and sort the groups in descending order according to the word length. 12 Console. writeLine (Environment. newLine + "display by length group:" + Environment. newLine); 13 var res1 = from s in words14 orderby s ascending // ascending in ascending order of 15 group s by s. length into lengroup16 orderby lengroup. key descending // descending in descending order 17 select new {Length = lengroup. key, Words = lengroup}; 18 foreach (var m in res1) 19 {20 Console. writeLine ("Word Length:" + m. length); 21 foreach (var str in m. 22 Console. writeLine ("" + str); 23}
The result is as follows:
Now, for List <> to process data, I only use LINQ to Objects.
(1.5) initial version of LINQ to XML
Code:
1 class Book 2 { 3 public string Publisher; 4 public string Title; 5 public int Year; 6 7 public Book(string title, string publisher, int year) 8 { 9 this.Title = title;10 this.Publisher = publisher;11 this.Year = year;12 }13 }
1 Book[] books = new Book[]{ 2 new Book("Ajax in Action","Manning",2005), 3 new Book("Windows Froms in Action","Manning",2006), 4 new Book("Rss and Atom in Action","Manning",2006) 5 }; 6 XElement xml = new XElement("books", 7 from s in books 8 where s.Year == 2006 9 select new XElement("book",10 new XAttribute("title", s.Title),11 new XElement("publisher", s.Publisher)12 ));13 Console.WriteLine(xml);
The result is as follows:
It seems that LINQ to XML is more expressive than DOM. This code and the XML structure it generates are basically "What you see is what you get.
In addition, LINQ to XML is element-centric, while DOM is document-centric.
(1.6) LINQ to SQL
The following code is described as follows:
1. Define an object class to associate with the Contacts data table of the Northwind data.
The Northwind data used in this article is demonstrated by Microsoft. You can go down and create one by yourself.
2. custom attribute [Table (Name = "Contacts")]. You must introduce System. data. linq. Otherwise, you will understand.
3. The custom attribute [Column (Name = "Name")] is used in the Code to define the correspondence with columns in the data table.
1 static class HelloLinqToSql 2 { 3 [Table(Name = "Contacts")] 4 public class Contact 5 { 6 [Column(IsPrimaryKey = true)] 7 public int ContactID { get; set; } 8 [Column(Name = "Name")] 9 public string Name { get; set; }10 [Column]11 public string City { get; set; }12 }13 }
4. DataContext is the device context. You must know that it is used to input a connection string.
5. security = SSPI in the connection string uses the security authentication mechanism provided by windows without entering the user name and password.
6. DataContext. GetTable <>. This is generic. Didn't you notice? Allows you to operate on strongly typed objects.
1 DataContext db = new DataContext (@ "server = (local); integrated security = SSPI; database = Northwind"); 2 3 var contacts = 4 from contact in db. getTable <HelloLinqToSql. contact> () 5 where contact. city = "Wuhan" 6 select contact; 7 8 Console. writeLine ("Finding contacts in Wuhan" + Environment. newLine); 9 foreach (var contact in contacts) 10 Console. writeLine ("contact:" + contact. name. trim () + "ID:" + contact. contactID );
The result is as follows:
This effect is really revolutionizing my understanding of relational database operations!
The following things are all contracted by LINQ:
- Open Database Connection
- Generate SQL query
- Execute SQL query
- Add execution results to objects
Original article, from "blog garden, pig weneng's blog": http://www.cnblogs.com/hackpig/