LINQ - into 子句

摘要:LINQ - into 子句

對於 LINQ 並不陌生,但也不一定熟悉到哪裡,所以最近又再看 LINQ 的書籍,其中一篇是介紹 into 子句,裡面的寫法自己沒寫過,就來記錄一下讓自己了解另一種寫法...

資料庫:Northwind 資料庫中的 dbo.Employees 資料表

Code:

namespace LINQ_IntoClause
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDataContext db = new MyDataContext();

            var result = from p in db.Employees
                         select new
                         {
                             LastName = p.LastName,
                             TitleOfCourtesy = p.TitleOfCourtesy
                         } into EmployeesList
                         orderby EmployeesList.TitleOfCourtesy ascending
                         select EmployeesList;

            foreach (var item in result)
            {
                Console.WriteLine("LastName:{0}、TitleOfCourtesy:{1}"
                    , item.LastName, item.TitleOfCourtesy);
            }

            Console.ReadKey();
        }
    }
}

單步執行:


結果:


參考:
LINQ 入門與應用