所以我得到了地址表和合同表。我想遍历所有合同并从中生成一些Dto,但要使用地址名称和编号而不是id(guid)。 我的问题是合同表中的地址表上有4个外键。我仍然可以通过某种方式甚至是多个联接来做到这一点吗?
这是我目前正在尝试的方法:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<div class="col-xs-6 date" style="display:block;">
<input class="form-control" type="datetime-local" id="inputDate" value="" />
</div>
我的另一种方法可能会生成太多查询:
var contracts = context.Contracts
.Join(context.Addresses,
(con => con.AddressId),
(adr => adr.AddressId),
(con,adr)
=> new { contractInfo = con, addressInfo = adr });
foreach(var a in contracts) {
new MyDto() {
AddressId = a.addressInfo.AddressNumber,
AddressName = a.addressInfo.FullName,
Date= a.contractInfo.Date,
AnotherDate = a.contractInfo.AnotherDate,
Text = a.contractInfo.Text,
VerweisAdrId = a.addressInfo. // ???
VerweisAdrName = a.addressInfo. // 2 more cases like this would follow
}
}
答案 0 :(得分:0)
var contracts = context.Contracts
.Join(context.Addresses,
(con => con.AddressId),
(adr => adr.AddressId),
(con,adr)
=> new { contractInfo = con, addressInfo = adr })
.Join(context.Addresses,
ca => ca.contractInfo.VerweisAdrId,
adr => adr.AddressId,
(ca, adr) => new { ca.contractInfo, ca.addressInfo, verweisAdr = adr })
// ... and so on
.Select(cavwx => new MyDto() {
AddressId = cavwx.addressInfo.AddressNumber,
AddressName = cavwx.addressInfo.FullName,
VerweisAdrId = cavwx.verweisAdr.AddressNumber,
VerweisAdrName = cavwx.verweisAdr.FullName,
// ... and so on
});