LINQ (Filtering and sorting)
The following is reproduced from: http://www.cnblogs.com/xfrog/archive/2010/09/11/1824086.html
This article describes the underlying query for LINQ (All examples use LINQ to Object)
Before we do this, we will first create a data source for the example:
-
- Student class: Denotes student, including number, name and class
- Courses class: Indicates the course of the student's choice, including number, course name and hours
- Datacreator class: Static class, generating sample data through the Generatedata method
- The resulting data is as follows:
School Number name class course name hours
003 Wang 52 Class economics 20
003 Wang 52 Class enterprise management 20
003 Wang 52 Class financial management 30
002 John Doe First Class history 20
002 John Doe A class of politics 20
002 John Doe Language 30
001 31 Classes Math 20
001 31-Class Languages 20
001 31-Class Physics 15
Generate data in the Main method:
Hide line number copy code ? C #
list<list<student> ();
list<list<courses> ();
Datacreator.generatedata (students, courses);
Screening
The WHERE clause filters the data through a WHERE clause, followed by a condition, and the From object can be used as the subject of the WHERE clause to determine the condition.
In fact from. In.. Statement is similar to a foreach statement, such as
From student in students similar to foreach (Student student in students)
Student is an element in a students collection, you can usually use student as the basis for judgment in the where statement, for example:
Filter out all students in one class:
- Hide line number copy code ? C #
In students
"Class One"
Select student;
foreach (inquery)
{
Console.WriteLine (s);
}
To filter courses that are greater than or equal to 20 hours:
- Hide line number copy code ? C #
var query = in courses
-
where course. Credit >=
-
select course;
can use && or | | To connect multiple conditions, such as filtering out a class surnamed Zhang's classmates:
- Hide line number copy code ? C #
-
-
var query = in students
-
where student. class = =
-
select student;
Of course, it's not that a conditional statement can use only the From object, and it could use any object as a judge (the following LINQ only makes no sense): The condition current time equals the current time, this condition is always true , all the student information will be queried.
- Hide line number copy code ? C #
In students
DateTime.Now
Select student;
- The following example takes the number of students collections as a criterion, and when the number of stuents is greater than or equal to 3 o'clock, all student data is screened:
- Hide line number copy code ? C #
var query = in students
-
where students. count>=3
-
select student;
The following example filters students that exist in the IDs collection:
- Hide line number copy code ? C #
string[] ids = { "001", "003"};
-
var query = from student in students
-
where IDs. Contains (student.id)
-
select student;
query students who have elective language courses:
- Hide line number copy code ? C #
var query = from student in students
-
where (from cours in courses Where Cours. Name = = select cours. StudentID). Contains (student.id)
-
select student;
-
-
var s in query)
-
{
-
console.writeline (s);
-
}
Note that the nested query is used in the previous example, and the students where statement contains another LINQ statement that returns the number of the language course taken from the courses collection, which is a ienumerable<string> collection, Therefore, you can use the IEnumerable contains method on this collection to determine whether the collection contains the ID of the current student object, including the return true, that is, the students where statement is true, and the student object is added to the result collection. otherwise not added.
Sort
The sort statement is simple and the basic syntax is:
By: Ascending | Descending[,.. Ascending | Descending]
Where ascending indicates ascending order, descending means descending order
Hide line number copy code ? C #
In students
/*descending*/
Select student;
Can be sorted according to multiple fields:
Hide line number copy code ? C #
In students
Descending
Select student;
Cond...
The following source code produces sample data for this article:
Hide line number copy code ? C #
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Linqstudy
{
Datacreator
{
public static void Generatedata (list<student> students,list<courses> Courses)
{
Students. ADD (Student ("001","one Shift"));
Students. ADD (Student ("one Shift"));
Students. ADD (Student ("Class II"));
Courses. ADD (Courses ("math"));
Courses. ADD (Courses ("language"));
Courses. ADD (Courses ("Physical",));
Courses. ADD (Courses ("History"));
Courses. ADD (Courses ("politics"));
Courses. ADD (Courses ("language");
Courses. ADD (Courses ("economics"));
Courses. ADD (Courses ("Enterprise Management",));
Courses. ADD (Courses ("financial Management",));
}
}
Student
{
Set }
Set }
Set }
Public Student (string ID,string c)
{
id = ID;
name = name;
Class = C;
}
public override string ToString ()
{
String.Format (This. Class);
}
}
Courses
{
Set }
Set }
Set }
Public Courses (String ID,int credits)
{
StudentID = ID;
name = name;
Credit = credit;
}
public override string ToString ()
{
String.Format (This. Credit);
}
}
}
LINQ (Filtering and sorting)