Notes for learning from linq into linear programming
I have recently learned some basic knowledge about linq, read c # advanced programming, and read several excellent blog posts in the garden. I have some experience and I feel that I should record it and use it for later review. These are some of the most basic knowledge, which can be roughly divided into three parts: the preparation knowledge of linq; the query by linq; and the query by linq to SQL. You can take a look at it. You can give me some advice. Thank you.
I. Preparation of linq:
1. implicit type
Before the implicit type occurs, the method is
int a = 10;string str = "abc";
Use the implicit type. The Code is as follows:
var a = 10;var str = "abc";
Don't worry that using var will affect its performance, because int and var are translated into the same intermediate language.
2. Anonymous type
var obj = new { id = 2, name = "tom" }; Console.WriteLine(obj.name);
In the above Code, the type is defined anonymously and the object obj is constructed. Then, its attributes can be called directly without worrying about any other problems.
3. Automatic attributes
Previously, attributes were defined for a class:
private string name; public string Name { get { return name; } set { name = value; } }
C #3.0 use automatically implemented attributes:
public string Name { get; set; }
4. Expansion Method
In many cases, you need to perform some operations on the CLR type, so that you cannot extend the CLR type method. You can only create one helper method or subclass, and extend the method to implement these requirements. The specific example is as follows:
// Non-nested, non-generic static class public static class EntendMethod {// static method, at least one parameter, the first parameter must be prefixed with this, and the first parameter cannot have other modifiers public static void PrintString (this String val) {Console. writeLine (val );}}
Call the extension method:
Var str = "aaa"; str. PrintString (); // call the Extension Method
5. Object Initiator
The Code is as follows:
Public class Student {public int ID {get; set;} public string Name {get; set;} public int Age {get; set ;}} // List <Student> students = new List <Student> (); students. add (new Student {ID = 1, Name = "tom", Age = 21}); students. add (new Student {ID = 2, Name = "tom2", Age = 22}); students. add (new Student {ID = 3, Name = "tom3", Age = 23 });
6. Set Initiator
Use the aforementioned Student class:
// List <Student> students2 = new List <Student> {new Student {ID = 1, Name = "tom", Age = 21} by using the set initiator }, new Student {ID = 2, Name = "tom2", Age = 22}, new Student {ID = 3, Name = "tom3", Age = 23 }};
7. other basic knowledge:
For Delegation, generic delegation, anonymous methods, and Lambda expressions, please refer to the delegated learning notes. Follow-up: anonymous methods and Lambda expressions involved in generic delegation and Delegation
2. query by linq
1. Basic Query
Use the preceding collection students for simple query:
// Simple query var studentSelect = from s in students where s. age> 22 select s; foreach (var s in studentSelect) {Console. writeLine (s. name + ":" + s. age );}
2. generate a new type object
// Construct the new object var items = from s in students select new {bianhao = s. ID, nianlian = s. age}; foreach (var item in items) {Console. writeLine (item. bianhao + ":" + item. nianlian );}
3. Use where filter for query
You can use the above (2) instance to continue:
var items = from s in students where s.ID > 1 select new { bianhao = s.ID, nianlian = s.Age }; foreach (var item in items) { Console.WriteLine(item.bianhao + ":" + item.nianlian ); }
4. Use index Filtering
However, in some cases, you may not be able to use the linq query. In this case, you cannot use the index to process the Where () method. The Code is as follows:
var items2 = students .Where((r, index) => r.ID != 5 && index % 2 != 0); foreach (var item in items2) { Console.WriteLine(item.ID + " " + item.Name ); }
5. Type Filtering
object[] objs = { "a", 2, 4, "b", 9, "d" }; var item3 = objs.OfType<int>(); foreach (var item in item3) { Console.WriteLine(item); }
6. Composite from clause
You can use the from clause to apply the content of the from clause in the query.
7. Sorting
Orderby s. ID descending. The specific instance is as follows:
var items4 = from s in students where s.Age > 22 orderby s.ID descending select s; foreach (var s in items4) { Console.WriteLine(s.Name + ":" + s.Age); }
Var items5 = students. orderBy (r => r. age); // ascending var items6 = students. orderByDescending (r => r. age); // descending foreach (var s in items5) {Console. writeLine (s. name + ":" + s. age );}
8. Aggregate Operators
Count (), Sum Min Max Average Aggregate returns a value instead of a sequence. Example:
int[] nums = new int[] { 1, 2, 3, 5, 12, 18, 23 }; var num = from n in nums select n; int result = num.Sum(); Console.WriteLine(result); var items7 = from s in students select s; var itemValue = items7.Max(m => m.Age); Console.WriteLine(itemValue);
Iii. linq to SQL
I have never been clear about what is going on with linq to SQL, so I will record my understanding of it and correct it later if I have a deeper understanding.
Open the server resource manager and add the following data tables. As follows:
Simple explanation: the database table Guest is the Guest's ID, name, age, and Room ID (R_ID of the Room table). The Room table has the ID, name, and price.
Query the name and price of a room with a price greater than 28:
public void SelectRoom() { HotelDataContext db = new HotelDataContext(); var rooms = from r in db.Room where r.R_Price > 28 select r; foreach (var r in rooms) { Console.WriteLine("RoomName:" + r.R_Name + " Room Price:" + r.R_Price); } }
Join Table query: query the name, age, room name, and price of a guest. The Code is as follows:
public void SelectGuestRoomInfo() { HotelDataContext db = new HotelDataContext(); var guestInfo = from g in db.Guest join r in db.Room on g.G_RoomID equals r.R_ID select new { name = g.G_Name, age = g.G_Age, roomName = r.R_Name, price = r.R_Price, }; foreach (var g in guestInfo) { Console.WriteLine("name:" + g.name + " age:" + g.age + " roomName:" + g.roomName + " price:" + g.price); } }
This is the end of the process. If you have any in-depth understanding, you can change and Add.