Blogs YAM

Tìm kiếm nội dung



January
3

LINQ Example

Chuyên mục : C# - viết bởi : Mr.Storm


Bạn có thể xem lại Khái niệm ở đây

 Bài tập của chúng ta ở đây sẽ có 2 bảng dữ liệu giả do chúng ta tạo ra bằng những IEnumerable

Bước 1 : Chúng ta tạo ra 1 class Customer bao gồm Name Id

class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Bước 2 : Chúng ta tạo ra thêm 1 class Order bao gồm có CustomerId Price

class Order
    {
        public int CustomerId { get; set; }
        public double Price { get; set; }
    }

Bước 3 : Chúng ta phải tạo dữ liệu giả cho từng class đó , chúng ta tạo thêm class DataSource

class DataSource
    {
        ///<summary>    
        /// Chúng ta khởi tạo data giả cho chương trình
        ///</summary>
        ///
        public static IEnumerable<Customer> Customers = new List<Customer>()
        {
            new Customer() {Name = "Mr Storm", Id = 1},
            new Customer() {Name = "Tieu Tinh" , Id = 2},
            new Customer() {Name = "Mr Black", Id = 3},
            new Customer() {Name = "Mr Bi" ,Id = 4}
        };

        public static IEnumerable<Order> Orders = new List<Order>()
        {
            new Order{CustomerId = 1, Price = 3000},
            new Order{CustomerId = 2, Price = 5000},
            new Order{CustomerId = 4, Price = 2000},
            new Order{CustomerId = 3, Price = 1000},
            new Order{CustomerId = 3, Price = 6000}
        };
    }

Ở đây bạn nào ko hỉu thì comment hay vào google search nha , mình sẻ ko giải thích cái này ở đây nữa .

Bây giờ chúng ta đã có cấu trúc dữ liệu và bắt tay vào làm thôi ^^!

Bước 4 : Chúng ta kiếm tên nào bắt đầu bằng chử 'M' và cho xuất ra màng hình chúng ta làm như sau 

///<summary>
            /// Chúng ta kiếm tên bắt đầu với param name
            ///</summary>

            Console.WriteLine("---- Kiem ten bat dau 'M'  ----");
            var customerResult = from c in DataSource.Customers
                                where c.Name.StartsWith("M")
                                select new { c.Name , c.Id , Full = c.Id + " : " + c.Name};
            foreach (var c in customerResult) 
            {
                Console.WriteLine(c.Full);
            }

Bước 5 : Sắp xếp các giá trị của trường Price  lại

///<summary>
            /// Chúng ta sắp xếp Price lại
            /// descending giảm dần
            /// ascending tăng dần
            ///</summary>
            Console.WriteLine("----  Sap xep giam dan ----");
            var sortResult = from o in DataSource.Orders
                             orderby o.Price descending
                             select o;

            foreach (var o in sortResult)
            {
                Console.WriteLine(o.Price);
            }

Bước 6 : Nhóm số tiền theo CustomerId lại 

 ///<summary>
            /// Nhóm số tiền customerID hiện có
            ///</summary>
            Console.WriteLine("---- Nhom so tien cua ID ----");
            var orderGroups = from o in DataSource.Orders
                              group o by o.CustomerId into g
                              select g;

            foreach (var item in orderGroups)
            {
                Console.WriteLine("So tien cua ID : " + item.Key);
                foreach(var order in item) 
                {
                    Console.WriteLine(order.Price);
                }
            }

Bước 7 : Cuối cùng chúng ta thử liên kết 2 bảng Customer Order lại theo Id và CustomerId 

///<summary>
            /// Liên kết Customers và Orders lại
            /// Id là primary key
            /// CustomerId là foreign key
            ///</summary>
            Console.WriteLine("---- Lien ket Customer va Orders ----");
            var result = from c in DataSource.Customers
                         join o in DataSource.Orders
                         on c.Id equals o.CustomerId
                         select new { c.Id, c.Name, o.Price };

            foreach (var r in result)
            {
                Console.WriteLine(r.Id + " : " + r.Name + " : " + r.Price);
            }

Các bạn tự tìm hỉu thêm nha ... Source Code bạn có thể down tại đây => Click here


Tags : LINQ Example,
Ý kiến / Bình luận
Gửi bình luận






Gửi ý kiến

Chuyên mục

Xem nhiều nhất

Bài viết nổi bật

Xem bài viết theo tháng

Danh sách Tags