这天主要是加强前些天的学习,主要学习各种Select 操作:
1. 匿名类
2.条件三目运算
三目运相翻译成SQL 的{case when condition=true a then else b}
3.Shaped形式
var s = from c in db.Customers
select new
{
c.CustomerID,
CompanyInfo = new { c.CompanyName, c.City, c.Country },
ContactInfo = new { c.ContactName, c.ContactTitle }
};
该形式,其select操作使用了匿名对象,而这个匿名对象中,其属性也是个匿名对象
效果如下图:
从图中可知信息为:顾客ID和公司信息(公司名称,城市,国家)和联系信息(联系人和职位)。
4.Distinct形式
var s = (from q in db.Customers
select q.City).Distinct();
它相当于SQL:Select Distinct City From Customers
5.嵌套类型形式
SalesInfoDataContext si = new SalesInfoDataContext();
var s = from o in si.Orders
select new
{
o.OrderID,
DiscountedProducts = from od in si.OrderDetails
where od.Discount > 0.0
select od,
FreeShippingDiscount = o.Freight
};
this.dataGridView1.DataSource = s;
返回的对象集中的每个对象DiscountedProducts属性中,又包含一个集合。也就是每个对象也是一个集合类
6.Where 条件过滤
简单过滤
var s = from p in db.Employees
where p.HireDate >= new DateTime(1900, 1, 1)
select p;
And 过滤
var s = from p in db.Products
where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
select p;
var s = db.Products.Where(p => p.UnitsInStock <= p.ReorderLevel).Where(p=>p.Discontinued);
上面语句会翻译为下面语句再翻译为SQL,送回数据服务器.上面两语句是等效的
1. 匿名类
- var s = from p in db.Customers
- select new { p.ContactName, p.Phone };
- var ev = from ep in edb.Employees
- slect new { name = ep.LastName + " " + ep.FirstName, phone = ep.HomePhone };
- var s=from p in db.Products
- select new { p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Stock" : "In Stock" };
3.Shaped形式
var s = from c in db.Customers
select new
{
c.CustomerID,
CompanyInfo = new { c.CompanyName, c.City, c.Country },
ContactInfo = new { c.ContactName, c.ContactTitle }
};
该形式,其select操作使用了匿名对象,而这个匿名对象中,其属性也是个匿名对象
效果如下图:
从图中可知信息为:顾客ID和公司信息(公司名称,城市,国家)和联系信息(联系人和职位)。
4.Distinct形式
var s = (from q in db.Customers
select q.City).Distinct();
它相当于SQL:Select Distinct City From Customers
5.嵌套类型形式
SalesInfoDataContext si = new SalesInfoDataContext();
var s = from o in si.Orders
select new
{
o.OrderID,
DiscountedProducts = from od in si.OrderDetails
where od.Discount > 0.0
select od,
FreeShippingDiscount = o.Freight
};
this.dataGridView1.DataSource = s;
返回的对象集中的每个对象DiscountedProducts属性中,又包含一个集合。也就是每个对象也是一个集合类
6.Where 条件过滤
简单过滤
var s = from p in db.Employees
where p.HireDate >= new DateTime(1900, 1, 1)
select p;
And 过滤
var s = from p in db.Products
where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
select p;
var s = db.Products.Where(p => p.UnitsInStock <= p.ReorderLevel).Where(p=>p.Discontinued);
上面语句会翻译为下面语句再翻译为SQL,送回数据服务器.上面两语句是等效的