使用LINQ查询操作[已关闭]

oewdyzsn  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(126)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
将此SQL查询转换为LINQ查询时,请获得帮助

SELECT EconCode.EconCodeId, 
       EconCode.EconCodeName, 
       EconCode.EconIncomeAmt, 
       EconCode.EconRecExpAmt, 
       EconCode.EconCapExpAmt, 
       FundCode.FundCodeId
FROM EconCode 
LEFT OUTER JOIN FundCode 
   ON EconCode.FundCodeId = FundCode.FundCodeId
GROUP BY EconCode.EconCodeId, 
         EconCode.EconCodeName, 
         EconCode.EconIncomeAmt, 
         EconCode.EconRecExpAmt, 
         EconCode.EconCapExpAmt, 
         FundCode.FundCodeId

我以前尝试过以下代码,但在我的报告中没有显示任何内容。

var query = from econ in _context.EconCode
         join fund in _context.FundCode on econ.FundCodeId equals fund.FundCodeId
         group new { econ, fund.FundCodeId } by new { fund.FundCodeName, econ.FundCodeId, econ.EconCodeId, econ.EconCodeName, econ.EconIncomeAmt, econ.EconRecExpAmt, econ.EconCapExpAmt  } into g
          select new
             {
              fundcodeId = g.Key.FundCodeId,
              fundCodeName = g.Key.FundCodeName,
              econCodeId = g.Key.EconCodeId,
              econCodeName = g.Key.EconCodeName,
              econIncomeAmt = g.Key.EconIncomeAmt,
              econRecExpAmt = g.Key.EconRecExpAmt,
              econCapExpAmt = g.Key.EconCapExpAmt,
          };
anauzrmj

anauzrmj1#

假设有两个名为EconCode和FundCode的对应实体类,上下文变量为dbContext,则提供的SQL查询的等效LINQ查询为:

var query = from econ in dbContext.EconCode
            join fund in dbContext.FundCode on econ.FundCodeId equals fund.FundCodeId into joinedGroup
            from fund in joinedGroup.DefaultIfEmpty()
            group new { econ, fund } by new { econ.EconCodeId, econ.EconCodeName, econ.EconIncomeAmt, econ.EconRecExpAmt, econ.EconCapExpAmt, FundCodeId = (int?)fund.FundCodeId } into g
            select new {
                g.Key.EconCodeId,
                g.Key.EconCodeName,
                g.Key.EconIncomeAmt,
                g.Key.EconRecExpAmt,
                g.Key.EconCapExpAmt,
                FundCodeId = g.Key.FundCodeId ?? 0
            };

相关问题