LINQ (Filtering and sorting)

Source: Internet
Author: User

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 #
      1. list<list<student> (); 
      2. list<list<courses> (); 
      3. 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 #
      1. In students
      2.             "Class One"
      3.             Select student;
      4. foreach (inquery)
      5. {
      6.     Console.WriteLine (s);
      7. }

    To filter courses that are greater than or equal to 20 hours:

  • Hide line number copy code ? C #
    1. var query = in courses    
    2.  where course. Credit >=  
    3.  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 #
    1.  var query = in students    
    2.  where student. class = =    
    3.  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 #
    1. In students
    2.             DateTime.Now
    3.             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 #
    1. var query = in students    
    2.  where students. count>=3  
    3.  select student;  

         The following example filters students that exist in the IDs collection:

  • Hide line number copy code ? C #
    1. string[] ids = { "001",  "003"};    
    2.  var query = from student in students    
    3.  where IDs. Contains (student.id)  
    4.  select student;  

         query students who have elective language courses:

  • Hide line number copy code ? C #
      1. var query = from student in students    
      2.  where (from cours in courses  Where Cours. Name = = select cours. StudentID). Contains (student.id)       
      3.  select student;  
      4. var s in query)    
      5.  {
      6.  console.writeline (s);  
      7. } 

    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 #
    1. In students
    2.             /*descending*/
    3.             Select student;

Can be sorted according to multiple fields:

Hide line number copy code ? C #
    1. In students
    2.             Descending
    3.             Select student;

Cond...

The following source code produces sample data for this article:

Hide line number copy code ? C #
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Namespace Linqstudy
  6. {
  7.     Datacreator
  8.     {
  9.         public static void Generatedata (list<student> students,list<courses> Courses) 
  10.         {
  11.             Students. ADD (Student ("001","one Shift"));  
  12.             Students. ADD (Student ("one Shift")); 
  13.             Students. ADD (Student ("Class II")); 
  14.             Courses. ADD (Courses ("math")); 
  15.             Courses. ADD (Courses ("language")); 
  16.             Courses. ADD (Courses ("Physical",)); 
  17.             Courses. ADD (Courses ("History")); 
  18.             Courses. ADD (Courses ("politics")); 
  19.             Courses. ADD (Courses ("language"); 
  20.             Courses. ADD (Courses ("economics")); 
  21.             Courses. ADD (Courses ("Enterprise Management",)); 
  22.             Courses. ADD (Courses ("financial Management",)); 
  23.         }
  24.     }
  25.     Student
  26.     {
  27.         Set }
  28.         Set }
  29.         Set }
  30.         Public Student (string ID,string c) 
  31.         {
  32.             id = ID;
  33.             name = name;
  34.             Class = C;
  35.         }
  36.         public override string ToString ()
  37.         {
  38.             String.Format (This. Class);
  39.         }
  40.     }
  41.     Courses
  42.     {
  43.         Set }
  44.         Set }
  45.         Set }
  46.         Public Courses (String ID,int credits) 
  47.         {
  48.             StudentID = ID;
  49.             name = name;
  50.             Credit = credit;
  51.         }
  52.         public override string ToString ()
  53.         {
  54.             String.Format (This. Credit);
  55.         }
  56.     }
  57. }

LINQ (Filtering and sorting)

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.