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的條數。
