我有具体问题。我对SQL不太熟悉,所以这可能是一个愚蠢的问题。我有以下课程
class StructuredResult
{
public Pay Pay{ get; set; }
public Processing Processing{ get; set; }
public Member Member { get; set; }
public StructuredResult()
{
}
}
这是我用
获得此代码的代码var allPaying = (from pay in Entities.Pays
join prc in Entities.Processingss on pay.IDCalculation equals cal.IDCalculation
join mbr in Entities.Members on pay.IDMember equals mbr.IDMember
where pay.IDMember == IDMember
orderby prc.DateFrom descending
select new StructuredResult()
{
Pay = pay,
Processing = prc,
Member = mbr
}).ToList();
代码工作正常我改变了在这里发布的东西,也许一些名称更改不行。我需要得到的是:
class StructuredResult
{
public Pay Pay{ get; set; }
public Processing Processing{ get; set; }
public Member Member { get; set; }
public List<PayDetail> PayDetails { get; set; }
public StructuredResult()
{
}
}
有谁知道这是否可能?付款详细信息与Pay.ID = PayDetail.ID相关联 提前致谢
答案 0 :(得分:1)
为了使用StructuredResult
的{{1}}集合填充PayDetail
,您需要使用Group Join
。参见:
GroupJoin
所以:
var allPaying = from pay in Entities.Pays
join prc in Entities.Processingss on pay.IDCalculation equals cal.IDCalculation
join mbr in Entities.Members on pay.IDMember equals mbr.IDMember
join d in Entities.PayDetails on pay.IDMember equals d.ID into details
where pay.IDMember == IDMember
orderby prc.DateFrom descending
select new StructuredResult()
{
Pay = pay,
Processing = prc,
Member = mbr,
PayDetails = details
};
我还建议您使用EF查看导航属性。通过稍微不同地定义类,您将获得更多的功能,而不是更少(例如,不需要编写连接)
只是一个小技巧 - 您正在定义一个id中没有逻辑的默认构造函数。如果在实际代码中也是如此,则不需要它。它是自动定义的。
答案 1 :(得分:1)
这可能会对您有所帮助:
var allPaying = (from pay in Entities.Pays
join prc in Entities.Processingss on pay.IDCalculation equals cal.IDCalculation
join mbr in Entities.Members on pay.IDMember equals mbr.IDMember
where pay.IDMember == IDMember
orderby prc.DateFrom descending
select new StructuredResult()
{
Pay = pay,
Processing = prc,
Member = mbr,
PayDetails = (from pd in Entities.PayDetails
where pd.ID == pay.IDMember
select pd).ToList())
}).ToList();