如果你帮助我,我会很高兴的
我有4张桌子:
http://i.stack.imgur.com/MMIdn.png
我需要从Condo获得ApptNumber,从Contract获得ContractID 如果没有合同分配给公寓,请将其留空。
像这样:
| Cnds | Cntrcts |
| -------- | ----------- |
| 101 | contr1 |
| 102 | contr2 |
| 103 | contr3 |
| 104 | |
| 105 | |
| 106 | contr4 |
| 107 | |
| -------- | ----------- |
我试图这样做,但未能得到正确的结果
var q =
from c in condos
join b in buildings on c.buildingID equals b.BuildingId
join t in tenants on b.BuildingId equals t.buildingID
join ct in contracts on t.TenantID equals ct.tenantID into result
from subresult in result.DefaultIfEmpty()
select new
{
c.apptNumber,
contract = (subresult == null ? 0 : subresult.ContractID)
};
此致
答案 0 :(得分:0)
from c in context.Condos
join ct in context.Contracts on c.apptNumber equals ct.SuiteNo into j
//it could be another field for joining
from d in j.DefaultIfEmpty()
select new {c.apptNumber, Contract = d != null ? d.ContractId : null}
答案 1 :(得分:0)
谢谢你们。首先使用多个JOINS是我的错。
我解决了这个问题:
var q =
from c in condos
join ct in contracts on c.apptNumber equals ct.SuiteNo into j //it could be another field
from d in j.DefaultIfEmpty()
select new { c.apptNumber, Contract = d != null ? d.ContractID : 0 };
var q2 = q.GroupBy(c => c.apptNumber).Select(group => group.First());