NHibernate Linq 的 join (联合查询) 的例子

1. 如果一个表中的id为非空而另一个表的id为可空 如何链接
var q =
from o in db.Orders
join e in db.Employees
on o.Employee.EmployeeId equals (int?) e.EmployeeId into emps
from e in emps
select new {o.OrderId, e.FirstName};

2. 用where连接

var q =
from e1 in db.Employees
from e2 in e1.Subordinates
where e1.Address.City == e2.Address.City
select new
{
FirstName1 = e1.FirstName,
LastName1 = e1.LastName,
FirstName2 = e2.FirstName,
LastName2 = e2.LastName,
e1.Address.City
};

3.用join后的结果统计

var q =
from c in db.Customers
join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into orders
select new {c.ContactName, OrderCount = orders.Average(x => x.Freight)};
var q =
from c in db.Customers
join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into ords
join e in db.Employees on c.Address.City equals e.Address.City into emps
select new {c.ContactName, ords = ords.Count(), emps = emps.Count()};

4.group join

var q = from c in db.Customers
join o in db.Orders on c.CustomerId equals o.Customer.CustomerId
group new { c, o } by c.ContactName
into g
select new { ContactName = g.Key, OrderCount = g.Average(i => i.o.Freight) };  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值