Truy vấn dữ liệu với LinQ
Lấy mẫu tin đầu tiên trong bảng(Lấy sản phẩm đầu tiên trong bảng Products). Hai câu truy vấn sau đây trả về cùng kết quả.
var products = context.Products.FirstOrDefault();Lấy mẫu tin cuối cùng(Sản phẩm cuối cùng trong bảng Products).
var products = (from p in context.Products select p).Skip(0).Take(1); //Tương đương SELECT TOP 1 trong SQL
var products = context.Products.LastOrDefault(); //LastOrDefault vẫn chưa đươc hỗ trợ trong .NET 3.5Lấy sản phẩm cố định trong bảng Products.
var products = (from p in context.Products orderby p.ProductID descending
select p).Skip(0).Take(1);
Product products = context.Products.Single(p=>p.ProductID == 22);Lấy 10 sản phẩm đầu tiên trong bảng Products.
var products = (from p in context.Products select p).Take(10);Lấy sản phẩm thứ 11 đến 20.
//Tương đươngSELECT TOP 10 trong SQL
var products = (from p in context.Products select p).Skip(10).Take(10);Lấy tất cả các sản phẩm trong bảng Products.
var products = from p in context.Products select p;Lấy các cột cố định trong bảng Products.
//Tương đương SELECT * trong SQL
var products = from p in context.Products select newThay đổi tiêu đề cột.
{
p.ProductID,
p.ProductName,
p.UnitPrice,
p.UnitsInStock,
p.UnitsOnOrder
};
var products = from p in context.Products select newSắp xếp các cột trong kết quả trả về.
{
MaSanPham = p.ProductID,
TenSanPham =p.ProductName,
DonGia = p.UnitPrice,
SoLuong =p.UnitsInStock,
SoLuongBan = p.UnitsOnOrder
};//Tương đương Alias columns
var products = from p in context.ProductsLoại bỏ các dòng dữ liệu trùng nhau.
orderby p.ProductID descending, p.ProductName//Tương đương ORDER BY trong SQL
select new
{
p.ProductID,
p.ProductName,
p.UnitPrice,
p.UnitsInStock,
p.UnitsOnOrder
};
var products = (from od in context.Order_Details join p in context.Products on od.ProductID equals p.ProductIDLấy hóa đơn có sản phẩm bán là 7(ProductID = 7)
select new { od.ProductID, p.ProductName}).Distinct();
//Tương đương SELECT DISTINCT trong SQ
var customers = from od in context.Order_Details where od.ProductID == 7Lấy sản phẩm có đơn giá trong khoảng 20-30.
select od;
var products = from p in context.ProductsLấy tất cả các sản phẩm đã bán ra.
where p.UnitPrice >= 20 && p.UnitPrice <= 30//Tương đương BETWEEN trong SQL
select new { p.ProductName, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder };
var products = from p in context.Products where (from od in context.Order_Detailsselect od.ProductID).Contains(p.ProductID)Lấy tất cả các sản phẩm chưa bán ra.
select new {p.ProductID, p.ProductName, p.UnitPrice, p.UnitsInStock,
p.UnitsOnOrder };//Tương đương từ khóa IN trong SQL
var products = from p in context.ProductsLấy danh sách khách hàng có tên bắt đầu là “Sa”.
where !(from od in context.Order_Details
select od.ProductID).Contains(p.ProductID)
//Tương đương từ khóa NOT IN trong SQL
select new {
p.ProductID, p.ProductName, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder };
var customers = from c in context.CustomersLấy danh sách khách hàng có tên kết thúc bằng “es”.
where c.CompanyName.StartsWith("Sa")//Tương đương LIKE 'Sa%' trong SQL
select new { c.CustomerID, c.CompanyName, c.ContactName, c.City, c.Country };
var customers = from c in context.CustomersLấy danh sách khách hàng có chứa chuỗi “sa” trong CompanyName.
where c.CompanyName.EndsWith("es")//Tương đương LIKE '%es' trong SQL
select new { c.CustomerID, c.CompanyName, c.ContactName, c.City, c.Country };
var customers = from c in context.CustomersLấy danh sách khách hàng tại Anh và Pháp.
where c.CompanyName.Contains("Sa")//Tương đương LIKE '%Sa%' trong SQL
select new { c.CustomerID, c.CompanyName, c.ContactName, c.City, c.Country };
var customers = (from c in context.CustomersLấy danh sách khách hàng có hóa đơn.
where c.Country == "UK"
select new
{
c.CustomerID,
c.CompanyName,
c.ContactName,
c.City, c.Country
}).Union(//Tương đương PHÉP HỢP UNION trong SQL
from c in context.Customers
where c.Country == "France"
select new {
c.CustomerID,
c.CompanyName,
c.ContactName,
c.City,
c.Country } );
var customers = from c in context.CustomersLấy hóa đơn của khách hàng, bao gồm khách hàng không có hóa đơn.
join o in context.Orders on c.CustomerID equals o.CustomerID
//Tương đương INNER JOIN trong SQL
orderby c.CustomerID, o.OrderID
select new {
c.CustomerID,
o.OrderID,
OrderDate = o.OrderDate.Value.ToShortDateString(),
c.CompanyName,
c.City,
c.Country };
var customers = from c in context.CustomersĐếm số hóa đơn đối với mỗi khách hàng
join o in context.Orders
on c.CustomerID equals o.CustomerID into OD
from o in OD.DefaultIfEmpty()//Tương đương OUTER JOIN trong SQL
select new
{
CustomerID = c.CustomerID,
CompanyName = c.CompanyName,
OrderID = o == null ? 0 : o.OrderID
};
var customers = from c in context.CustomersĐếm số hóa đơn đối với khách hàng có lớn hơn 5 hóa đơn.
select new { c.CustomerID, c.CompanyName, c.Orders.Count };
var customers = from c in context.CustomersĐếm số hàng hóa trên mỗi hóa đơn và tính đơn giá bình quân của hóa đơn.
where c.Orders.Count >5
select new {c.CustomerID, c.CompanyName, c.Orders.Count };
var orders = from od in context.Order_DetailsĐếm số hàng hóa trên mỗi hóa đơn và tính giá trị hóa đơn.
group od by od.OrderID into OD//Tương đương GROUP BY trong SQL
orderby OD.Key
select new
{
OrderID = OD.Key,
ProductID = OD.Count(),
UnitPrice = OD.Average(m => m.UnitPrice)
};
var orders = from od in context.Order_DetailsLấy sản phẩm có giá bán cao nhất và thấp nhất trong mỗi hóa đơn.
group od by od.OrderID into OD
orderby OD.Key
select new
{
OrderID = OD.Key,
Product = OD.Count(),
UnitPrice = OD.Sum(m => m.UnitPrice)
};
var orders = from od in context.Order_DetailsLấy giá bán cao nhất và thấp nhất của mỏi sản phẩm.
group od by od.OrderID into OD
orderby OD.Key
select new
{
OrderID = OD.Key,
MinPrice = OD.Min(m => m.UnitPrice),
MaxPrice = OD.Max(m => m.UnitPrice)
};
var orders = from od in context.Order_Details
join p in context.Products on od.ProductID equals p.ProductID
group od by new { p.ProductID, p.ProductName } into OD
orderby OD.Key.ProductID
select new
{
ProductID = OD.Key.ProductID,
ProductName = OD.Key.ProductName,
MaxPrice = OD.Max(m => m.UnitPrice),
MinPrice = OD.Min(m => m.UnitPrice)
};
Nguồn: http//vietshare.vn
Truy vấn dữ liệu với LinQ
Reviewed by 20Me Reviews
on
09:48
Rating: