- 1 1 var query1 = from r in _residentRepository.GetAll()
- 2 2 join i in _inLogRepository.GetAll() on r.Id equals i.ResidentId into tmp_ir
- 3 3 from ir in tmp_ir.DefaultIfEmpty()
- 4 4 where r.Id == 655 || r.Id == 654
- 5 5 select new
- 6 6 {
- 7 7 resId = r.Id,
- 8 8 Id = ir.Id
- 9 9 };
- 10 10 var sum1 = query1.Count();
- 结果:左连接 一对多 没毛病
- 1 var query = from r in _residentRepository.GetAll()
- 2 join i in _inLogRepository.GetAll() on r.Id equals i.ResidentId into tmp_ir
- 3 from ir in tmp_ir.DefaultIfEmpty().Take(1)
- 4 where r.Id == 655 || r.Id == 654
- 5 select new
- 6 {
- 7 resId = r.Id,
- 8 Id = ir.Id
- 9 };
- 10 var sum = query.Count();
结果:左连接 一对一

.Take()方法
由此看出 使用.Take(1)方法在多表关联后可以实现与主表一对一的关系数据来;根据业务需要也实现特定N的条数。