1. Trang chủ
  2. » Giáo Dục - Đào Tạo

BÀI tập TUẦN 8

7 40 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 91,41 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Hãy liệt kê tất cả các Product theo nhóm Category.. Hãy nhóm các đơn hàng của khách hàng theo năm và theo tháng.. Tìm tất cả các đơn hàng có tổng giá trị nhỏ hơn 500 triệu.. Tìm tất cả c

Trang 1

BÀI TẬP TUẦN 8 LINQ TO SQL

CÂU 01 Hãy liệt kê tất cả các Product

GIẢI

var c1a = Products.Select(i = > i);

var c1b = from item in Products

select item;

c1b.Dump();

CÂU 02 Hãy liệt kê tất cả các Product theo nhóm Category

GIẢI

var c2a = from item in Products

group item by item.Category;

c2a.Dump();

var c2b = Products.GroupBy(item = > item.Category);

CÂU 03 Hãy nhóm các đơn hàng của khách hàng theo năm và theo tháng

GIẢI

var c3a = from o in Orders

group o by new{ o.OrderDate.Year, o.OrderDate.Month };

var c3b = Orders.Group(o = > new {o.OrderDate.Year,

o.OrderDate.Month});

CÂU 04 Tìm tất cả các đơn hàng có tổng giá trị nhỏ hơn 500 triệu

GIẢI

var c4a = from order in Orders

Trang 2

join od in OrderDetails on o.OrderID equals od.OrderID into ood let total = new{ total = ood.Sum(i = > (1 - i.Discount)

*(float)(i.UnitPrice * i.Quantity)), id = o.OrderID }

where total.total < 500

select total;

c4a.Dump();

var c4b = OrderDetails.GroupBy(od = > od.OrderID)

.Select(item = > new {orderId = item.Key, total = item.Sum(w = > (1 - w.Discount) *(float)(w.UnitPrice * w.Quantity))})

.Where(item = > item.total < 500)

.Select(item = > item.orderId);

c4b.Dump();

CÂU 05 Tìm tất cả các đơn hàng được lập sau năm 1997

GIẢI

var c5a = from order in Orders

where order.OrderDate.Year < 1997

select order;

c4a.Dump();

var c5b = Orders.Where(o = > o.OrderDate.Year < 1997);

c4b.Dump();

CÂU 06 Tìm tất cả các đơn hàng có tổng giá trị lớn hơn 600 triệu

và được khách hàng từ USA đặt

GIẢI

var c6a = from o in Orders

join c in Customers on o.CustomerID equals c.CustomerID

join od in OrderDetails on o.OrderID equals od.OrderID into ood

Trang 3

let total = new{ total = ood.Sum(i = > (1 - i.Discount)

*(float)(i.UnitPrice * i.Quantity)), id = o.OrderID, c.Country }

where total.total > 600 && total.Country == "USA"

select total;

c6a.Dump();

var c6b = OrderDetails

.GroupBy(od = > od.OrderID)

.Select(item = > new {OrderID = item.Key, total = item.Sum(i = > (1 - i.Discount) *(float)(i.UnitPrice * i.Quantity))})

.Join(Orders, od = > od.OrderID, o = > o.OrderID, (od, o) = > new

{o.OrderID, od.total, o.CustomerID})

.Join(Customers, o = > o.CustomerID, c = > c.CustomerID, (o, c) =

> new {o.OrderID, c.Country, o.total})

.Where(item = > item.Country == "USA" && item.total > 600);

c6b.Dump();

CÂU 07 Hãy tìm các Category có ít nhất 1 Product hết hàng

GIẢI

var c7a = from p in Products

where p.UnitsInStock == 0

group p by p.CategoryID into pp

join c in Categories on pp.Key equals c.CategoryID

select c.CategoryName;

c7a.Dump();

var c7b = Products.Where(p = > p.UnitsInStock == 0)

.GroupBy(p = > p.CategoryID)

.Join(Categories, p = > p.Key, c = > c.CategoryID, (p, c) = > new

{name = c.CategoryName});

Trang 4

c6b.Dump();

CÂU 08 Hãy tìm các Category không có Product nào hết hàng

GIẢI

var c8a = from p in Products

where p.UnitsInStock > 0

group p by p.CategoryID into pp

select new{ Key = pp.Key, SLCH = pp.Count() } into w

join c in Categories on w.Key equals c.CategoryID

where c.Products.Count() == w.SLCH

select c.CategoryName;

c8a.Dump();

var c8b = Products

.Where(p = > p.UnitsInStock > 0)

.GroupBy(p = > p.CategoryID)

.Select(pc = > new {CategoryID = pc.Key, SLCH = pc.Count()}) .Join(Categories, p = > p.CategoryID, c = > c.CategoryID, (p, c)

= > new {p, c})

.Where(pc = > pc.p.SLCH == pc.c.Products.Count())

.Select(pc = > pc.c.CategoryName)

;

c8b.Dump();

CÂU 09 Hãy cho biết số lượng đặt hàng của mỗi khách hàng

GIẢI

var c9a = from o in Orders

group o by o.CustomerID into p

select new{ CustomerID = p.Key, SL = p.Count() } into k

Trang 5

join c in Customers on k.CustomerID equals c.CustomerID

select new{ k.SL, c.ContactName };

c9a.Dump();

var c9b = Customers.GroupJoin(Orders, c = > c.CustomerID, o = > o.CustomerID, (c, o) = > new{c.ContactName, sl = o.Count()}); c9b.Dump();

CÂU 10 Hãy cho biết số sản phẩm trong mỗi danh mục

GIẢI

var c10a = from c in Categories

group c by c.CategoryID into k

select new{ k.Key, SL = k.Count() } into gr

join c in Categories on gr.Key equals c.CategoryID

select new{ SL = gr.SL, c.CategoryName };

var c10b = Categories

.GroupBy(c = > c.CategoryID)

.Select(c = > new {ID = c.Key, SL = c.Count()})

.Join(Categories, c = > c.ID, d = > d.CategoryID, (c, d) = > new

{SL = c.SL, d.CategoryName});

CÂU 11 Hãy cho biết tổng số lượng sản phẩm trong mỗi danh mục

GIẢI

var c11a = from p in Products

group p by p.CategoryID into k

select new{ CategoryID = k.Key, sl = k.ToList().Sum(z = >

z.UnitsInStock) };

c11a.Dump();

Trang 6

var c11b = Products

.GroupBy(p = > p.CategoryID)

.Select(p = > new {ID = p.Key, sl = p.ToList().Sum(product = > product.UnitsInStock)});

CÂU 12 Hãy cho biết giá rẻ nhất(sản phẩm có giá rẻ nhất) trong mỗi danh mục

GIẢI

var c12a = from p in Products

group p by p.CategoryID into k

select new{ ID = k.Key, lowestPrice = k.ToList().Min(product = > product.UnitPrice) };

c12a.Dump();

var c12b = Products

.GroupBy(p = > p.CategoryID)

.Select(p = > new {ID = p.Key, lowestPrice =

p.ToList().Min(product = > product.UnitPrice)});

CÂU 14 Hãy cho biết giá đắt nhất(Sản phẩm có giá đắt nhất) trong mỗi danh mục

GIẢI

var c14a = from p in Products

group p by p.CategoryID into k

select new{ ID = k.Key, highestPrice = k.ToList().Max(product = > product.UnitPrice) };

c14a.Dump();

var c14b = Products

.GroupBy(p = > p.CategoryID)

Trang 7

.Select(p = > new {ID = p.Key, highestPrice =

p.ToList().Max(product = > product.UnitPrice)});

CÂU 16 Hãy cho biết sản phẩm đắt nhất trong mỗi danh mục

GIẢI

var c16a = from p in Products

group p by p.CategoryID into k

select new{ ID = k.Key, averagePrice = k.ToList().Average(product

= > product.UnitPrice) };

c16a.Dump();

var c16b = Products

.GroupBy(p = > p.CategoryID)

.Select(p = > new {ID = p.Key, averagePrice =

p.ToList().Average(product = > product.UnitPrice)});

Ngày đăng: 01/01/2020, 22:00

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w