Ngôn ngữ LINQ - Thao tác truy vấn dữ liệu

Thao tác truy vấn dữ liệu

Đoạn mã sau truy vấn và hiển thị dữ liệu:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var dataBook = new DataBookStoreDataContext())
            {
              
                //Truy Vấn Tác giả
                var author =  dataBook.Authors.ToList<Author>();
                Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("ID       |   Ten tac gia\t\t|   Email\t\t|   Diachi");
                Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------");
                foreach (var a in author) {               
                    Console.WriteLine("{0,5}    |   {1,-20}\t| {2,-10}\t| {3,20}    ", a.AuthorID, a.AuthorName,a.AuthorEmail,a.AuthorAddress);
                    Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------");
                }
                
            }
        }
       
    }
}

Kết quả:

dataBook.Authors.ToList<Author>(); Lấy hết tác giả đổi sang dạng ToList

Vì kết quả trả về là danh sách nên dùng foreach để lấy tất cả dữ liệu trong đó.