将具有多个连接的sql子查询转换为linq

时间:2015-04-27 16:28:11

标签: sql linq subquery

我在LINQ的子选择中遇到了麻烦。这是LINQ后跟的SQL。 SQL Query运行正常。问题是合并子查询。任何帮助将不胜感激:

SELECT DISTINCT
   cic.CommitmentItemCategoryName + '( ' + cicType.CommitmentItemCategoryTypeName + ' )' AS displayCategory
   , 'CategoryType_' + CAST(cic.CommitmentItemCategoryID AS VARCHAR(10)) AS displayCategoryID
   , ISNULL(vwPAD.DollarsAllocated, 0) AS DisplayDollarsAllocated
   ,cic.CommitmentItemCategoryID
FROM
   tblCommitmentItemCategory cic
LEFT OUTER JOIN 
   tblCommitmentItemCategoryType cicType ON cic.CommitmentItemCategoryTypeID = cicType.CommitmentItemCategoryTypeID
LEFT OUTER JOIN 
   tblAccountDirectToCommitmentItemCategory adToCIC ON adToCIC.CommitmentItemCategoryID IN (SELECT CommitmentItemCategoryID FROM tblCommitmentItemCategory)
LEFT OUTER JOIN 
   vw_ParentAccountDollarsAllocatedByCommitmentItemCategory vwPAD ON  vwPAD.FiscalYear = 2015 
                  AND cic.CommitmentItemCategoryID = vwPAD.CommitmentItemCategoryID
                  AND vwPAD.AccountDirectParentID = 19
WHERE 
    adToCIC.AccountDirectParentID = 19
ORDER BY
    displayCategory

var queryInner = from cic in MyContext.tblCommitmentItemCategory
                                 select new
                                 {
                                     cic.CommitmentItemCategoryID
                                 };

                var queryDollars = (from cic in MyContext.tblCommitmentItemCategory
                                   join cicType in MyContext.tblCommitmentItemCategoryType
                                   on cic.CommitmentItemCategoryTypeID equals cicType.CommitmentItemCategoryTypeID
                                   into t2
                                   from cicType in t2.DefaultIfEmpty()

                                   join adToCIC in MyContext.tblAccountDirectToCommitmentItemCategory
 //What goes here?
                                   on ...
                                  // on cic.CommitmentItemCategoryID equals     adToCIC.CommitmentItemCategoryID
                                   into t3
                                   from adToCIC in t3.DefaultIfEmpty()

                                   join vw in MyContext.vw_ParentAccountDollarsAllocatedByCommitmentItemCategory
                                   on new { FiscalYear = currentFiscalYear, CommitmentItemCategory = cic.CommitmentItemCategoryID, ParentAccountID = currentParentAccountID } 
                                    equals new { FiscalYear = vw.FiscalYear, CommitmentItemCategory = vw.CommitmentItemCategoryID, ParentAccountID = vw.AccountDirectParentID }
                                   into t
                                   from vw in t.DefaultIfEmpty()

                                   where adToCIC.AccountDirectParentID == currentParentAccountID

                                   let displayCategory = cic.CommitmentItemCategoryName + " ( " + cicType.CommitmentItemCategoryTypeName + " )"

                                   // Called CategoryType but it's actually the ID below

                                   let displayCategoryTypeID = "CategoryType_" + cic.CommitmentItemCategoryID.ToString()
                                   //let displayCategoryTypeID = "CategoryType!" + cic.CommitmentItemCategoryID + "_" + adToCIC.AccountDirectToCommitmentItemCategoryID.ToString()
                                   let displayDollarsAllocated = vw.DollarsAllocated == null ? 0 : vw.DollarsAllocated

                                   orderby cic.CommitmentItemCategoryName
                                   select new
                                   {
                                       displayCategory,
                                       displayCategoryTypeID,
                                       cic.CommitmentItemCategoryID,
                                       displayDollarsAllocated
                                   }).Distinct();

1 个答案:

答案 0 :(得分:0)

您可以将queryInner转换为加入吗?然后在较大的查询中使用该结果:

var queryInner = 
    from atcic in MyContext.tblAccountDirectToCommitmentItemCategory
    join cic in MyContext.tblCommitmentItemCategory on atcic.CommitmentItemCategoryID equals cic.CommitmentItemCategoryID
    select atcic;

在大问题中:

join adToCIC in queryInner into t3
    from adToCIC in t3.DefaultIfEmpty()
相关问题