程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> Linq學習(3) 語法結構

Linq學習(3) 語法結構

編輯:關於C#

Linq 語法結構

(來自Linq in action)

舉個例子實現:

查詢以a開頭的字符串,按降序輸出。

C#代碼:

string[] list = { "adobe", "excel", "acer", "query", "async", "xianfen", "apple", "amazon", "player", "best", "wow" };
var result = from word in list
       where word.StartsWith("a")
       orderby word descending
       select word;
result.ToList().ForEach(str => Console.WriteLine(str));

// 輸出:

// async
// apple
// amazon
// adobe
// acer

等價的面向對象語法為:

var result = list.Where(str => str.StartsWith("a"))
  .OrderByDescending(str => { return str; });

這裡 Where 和 OrderByDescending都是IEnumerable<T>的擴展方法,因為數組類型實現了該接口,所以可以直接調用這些這些擴展方法。

這些擴展方法的實現是在Enumerable類型中實現的,因此,更直接的是調用Enumerable類型的靜態方法方法:

var result =
  Enumerable.OrderByDescending(
  Enumerable.Where(
  list, str => str.StartsWith("a")), str => { return str; }
  );

從這些逆向演化可以看出,查詢語法就是一種"語法糖"。前面提到過,編譯後運行時版本還是2.0,從這可以看出一些端倪。如果想繼續深入了解該實現可以用ILDASM查看IL源代碼和反射類庫源代碼。

result類型為IEnumerable<TElement>或派生自IEnumerable<TElement>接口的類型。

與 Linq 相關的程序集

System.Linq 命名空間在System.Core.dll中,接口類型及擴展方法的定義在該命名空間中,如:

Enumerable
IGrouping<TKey, TElement>
ILookup<TKey, TElement>
IQueryable<T>
IOrderedQueryable<T>
Expressions
...

System.Data.DataSetExtensions.dll 包含 Linq to DataSet 的相關類型的定義。

System.Data.Linq.dll 包含 Linq to SQL 的相關類型的定義。

System.Xml.Linq.dll 包含 Linq to XML 的相關類型的定義。

全程數據

為了下面介紹方便,構造以下,

數據結構圖如下:

數據定義如下:

public class Student
{
  public int StudentID { get; set; }
  public string Name { get; set; }
  override public string ToString()
  {
    return string.Format("Student ID:{0},Student Name:{1}", StudentID, Name);
  }
}
public class Course
{
  public int CourseID { get; set; }
  public string CourseName { get; set; }
  override public string ToString()
  {
    return string.Format("Course ID:{0},Course Name:{1}", CourseID, CourseName);
  }
}
public class Score
{
  public int StudentID { get; set; }
  public int CourseID { get; set; }
  public double Value { get; set; }
  override public string ToString()
  {
    return string.Format("Student ID:{0},Course ID:{1},Score:{2}", StudentID, CourseID, Value);
  }
}
public class DataSource
{
  public static List<Student> Students { get; private set; }
  public static List<Student> Students2 { get; private set; }
  public static List<Score> Scores { get; private set; }
  public static List<Course> Courses { get; private set; }
  static DataSource()
  {
    Students = new List<Student>
    {
      new Student{ StudentID=1, Name="Andy" },
      new Student{ StudentID=2, Name="Bill" },
      new Student{ StudentID=3, Name="Cindy" },
      new Student{ StudentID=4, Name="Dark" }
    };
    Students2 = new List<Student>
    {
      new Student{ StudentID=5, Name="Erik" },
      new Student{ StudentID=6, Name="Frank" }
    };
    Courses = new List<Course>
    {
      new Course { CourseID = 1, CourseName = "C Language" },
      new Course { CourseID = 2, CourseName = "Biophysics" },
      new Course { CourseID = 3, CourseName = "Fundamentals of Compiling" },
      new Course { CourseID = 4, CourseName = "Finance" },
      new Course { CourseID = 5, CourseName = "College English" }
    };
    Scores = new List<Score>
    {
      new Score { StudentID = 1, CourseID = 1, Value = 78 },
      new Score { StudentID = 1, CourseID = 2, Value = 60 },
      new Score { StudentID = 1, CourseID = 3, Value = 92 },
      new Score { StudentID = 1, CourseID = 4, Value = 85 },
      new Score { StudentID = 1, CourseID = 5, Value = 55.5 },
      new Score { StudentID = 2, CourseID = 1, Value = 59 },
      new Score { StudentID = 2, CourseID = 2, Value = 78 },
      new Score { StudentID = 2, CourseID = 3, Value = 86 },
      new Score { StudentID = 3, CourseID = 2, Value = 60 },
      new Score { StudentID = 4, CourseID = 4, Value = 96 },
      new Score { StudentID = 5, CourseID = 5, Value = 78 }
    };
  }
}

後續知識點將使用這裡定義的數據進行解析。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved