如何使用组连接结果使用LINQ连接另一个表?

时间:2013-01-16 07:02:22

标签: sql linq c#-4.0 entity-framework-4

我无法使用组连接的结果,然后进一步将连接应用于其他表。

我的SQL查询是:

SELECT *
FROM   
    [SpectrumNew].[dbo].[MortgageApplication] M
INNER JOIN 
    SpectrumNew.dbo.Applicants A ON A.ApplicantMortgageApplicationId = M.MortgageApplicationId
INNER JOIN 
    SpectrumNew.dbo.SubjectProperty SP ON  SP.PropertyMortgageApplicationId = M.MortgageApplicationId
INNER JOIN 
    SpectrumNew.dbo.MortgageConditionsCommitment CC ON  CC.MortgageApplicationId = m.MortgageApplicationId
INNER JOIN 
    SpectrumNew.dbo.MortgageLoanStatusAndTrackingNotes T ON  T.MortgageTrackingMortgageApplicationId = M.MortgageApplicationId
INNER JOIN 
    (SELECT 
        MAX(MortgageTrackingDate) AS MaxDate,
        MortgageTrackingMortgageApplicationId
     FROM   
        [SpectrumNew].[dbo].[MortgageLoanStatusAndTrackingNotes]
     GROUP BY
        MortgageTrackingMortgageApplicationId) Q ON  Q.MortgageTrackingMortgageApplicationId = M.MortgageApplicationId

到目前为止我所做的是:

 var q = (from mortgageloan in context.MortgageLoanStatusAndTrackingNotes
                 group mortgageloan by mortgageloan.MortgageTrackingMortgageApplicationId into g
                 let maxdate = g.Max(date => date.MortgageTrackingDate)
                 select new
                 {
                     MortgId = g.Key,
                     TrackingDate = g.FirstOrDefault(val => val.MortgageTrackingDate == maxdate).MortgageTrackingDate

                 }
                 );

但是现在我没有进一步使用这个结果来创建与其他表的连接。我想问一下如何进一步加入?任何线索?

1 个答案:

答案 0 :(得分:1)

你需要这样的东西:

var groups = context.MortgageLoanStatusAndTrackingNotes
    .GroupBy(x => x.MortgageTrackingMortgageApplicationId)
    .Select(g => new 
    {
        MortgageTrackingMortgageApplicationId = g.Key,
        MaxDate = g.Max(date => date.MortgageTrackingDate)
    });

var result = context.MortgageApplications
    .Join(context.Applicants,
        m => m.MortgageApplicationId,
        a => a.MortgageApplicationId,
        (m, a) => new 
        {
            MortgageApplication = m,
            Applicant = a
        })
    .Join(context.SubjectProperties,
        x => x.MortgageApplication.MortgageApplicationId,
        sp => sp.PropertyMortgageApplicationId,
        (x, sp) => new
        {
            MortgageApplication = x.MortgageApplication ,
            Applicant = x.Applicant,
            SubjectProperty = sp
        })
    .Join(context.MortgageConditionsCommitments,
        x => x.MortgageApplication.MortgageApplicationId,
        cc => cc.MortgageApplicationId,
        (x, cc) => new
        {
            MortgageApplication = x.MortgageApplication ,
            Applicant = x.Applicant,
            SubjectProperty = x.SubjectProperty,
            MortgageConditionsCommitment = cc
        })
    .Join(context.MortgageLoanStatusAndTrackingNotes,
        x => x.MortgageApplication.MortgageApplicationId,
        t => t.MortgageTrackingMortgageApplicationId,
        (x, t) => new
        {
            MortgageApplication = x.MortgageApplication ,
            Applicant = x.Applicant,
            SubjectProperty = x.SubjectProperty,
            MortgageConditionsCommitment = x.MortgageConditionsCommitment,
            MortgageLoanStatusAndTrackingNotes = t
        })
    .Join(groups,
        x => x.MortgageApplication.MortgageApplicationId,
        g => g.MortgageApplicationId,
        (x, g) => new
        {
            MortgageApplication = x.MortgageApplication ,
            Applicant = x.Applicant,
            SubjectProperty = x.SubjectProperty,
            MortgageConditionsCommitment = x.MortgageConditionsCommitment,
            MortgageLoanStatusAndTrackingNotes = t,
            MortgageLoanStatusGroup = g
        });