如何检索一个表行和连接到Linq .NET中第一行的行列表

时间:2017-07-08 08:27:11

标签: c# sql .net entity-framework linq

我有具体问题。我对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相关联 提前致谢

2 个答案:

答案 0 :(得分:1)

为了使用StructuredResult的{​​{1}}集合填充PayDetail,您需要使用Group Join。参见:

  1. MSDN on GroupJoin
  2. 最后Linq to Entities join vs groupjoin
  3. 所以:

    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查看导航属性。通过稍微不同地定义类,您将获得更多的功能,而不是更少(例如,不需要编写连接)

    1. MSDN - Entity Framework Relationships and Navigation Properties
    2. MSDN - How to: Navigate Relationships Using Navigation Properties
    3. 只是一个小技巧 - 您正在定义一个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();