在一个键列上加入两个linq查询以获得一个结果集

时间:2017-03-20 03:25:20

标签: c# linq-to-entities

这里我想加入第一个查询(一列)的输出到第二个查询的结果以获得一个结果集。我如何合并它们。(CONCAT不能按要求工作。例如:var query2 = query.concat(query1);)

  var query = (from PP in _db.paymentPlans
                     join APP in _db.Applications on PP.applicationID equals      APP.ApplicationId
                     join C in _db.Courses on APP.courseID equals C.courseID
                     where PP.active == true && APP.agentID == agentID
                     orderby C.courseID ascending
                     group new {C,PP} by new {C.courseID} into totalRecievable
                      select new PdPpAppCourseModel
                     {
                         courseID = totalRecievable.Key.courseID,                            
                         totalAmount = totalRecievable.Sum(x => x.PP.totalAmount)
                     }).ToList();


            var query1=(from PD in _db.paymentDetails
                     join PP in _db.paymentPlans on PD.paymentPlanID equals PP.paymentPlanID
                     join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
                     join C in _db.Courses on APP.courseID equals C.courseID
                     where PP.active == true && APP.agentID == agentID
                     orderby C.courseID ascending
                     group new { C,PD } by new { C.courseID, C.cricosCode, C.courseName } into paymentsCourseWise

                     select new PdPpAppCourseModel
                     {
                         courseID = paymentsCourseWise.Key.courseID,
                         cricosCode = paymentsCourseWise.Key.cricosCode,
                         courseName = paymentsCourseWise.Key.courseName,
                         paidAmount = paymentsCourseWise.Sum(x => x.PD.paidAmount)

                     }).ToList();

1 个答案:

答案 0 :(得分:0)

你可以像这样加入query1和查询

 var result = (from q1 in query1
            join q in query on q1.courseID = q.courseID 
            select new PdPpAppCourseModel
            {
                courseID = q1.Key.courseID,
                cricosCode = q1.Key.cricosCode,
                courseName = q1.Key.courseName,
                paidAmount = q1.Sum(x => x.PD.paidAmount),
                totalAmount = q.totalAmount

            }).ToList();