.net 与实体中的排序依据联接-但对于已结余额,只需要最后日期

0qx6xfy6  于 2022-12-24  发布在  .NET
关注(0)|答案(1)|浏览(105)

我正在寻找帮助连接3个表,但从最后一个表,我只需要最后一个条目的日期为余额。我尝试OrderByDescending为GLBalances表。

var list = await (from ba in _context.BankAccounts
            join bnk in _context.Banks on ba.BankId equals bnk.ID
            join glB in _context.GLBalances on ba.BankGL equals glB.GLAccountGuid
            select new BankAccountDto()
            {
                BankId = ba.BankId,
                AccountNumber = ba.AccountNumber,
                BankName = bnk.BankName,
                Notes = ba.Notes,
                Description = ba.Description,
                Balance = (decimal)glB.ClosingBalance // ***need the last entry by date

            }).ToListAsync();
nqwrtyyt

nqwrtyyt1#

要按日期获取GLBalances表的最后一个条目,可以使用OrderByDescending方法按日期降序对GLBalances记录进行排序。
您可以修改查询以实现此目的:

var list = await (from ba in _context.BankAccounts
            join bnk in _context.Banks on ba.BankId equals bnk.ID
            join glB in _context.GLBalances
                .OrderByDescending(glb => glb.Date)
                .Where(glb => glb.GLAccountGuid == ba.BankGL)
                .Take(1) on ba.BankGL equals glB.GLAccountGuid
            select new BankAccountDto()
            {
                BankId = ba.BankId,
                AccountNumber = ba.AccountNumber,
                BankName = bnk.BankName,
                Notes = ba.Notes,
                Description = ba.Description,
                Balance = (decimal)glB.ClosingBalance
            }).ToListAsync();

更新了。试试这个,

var list = await (from ba in _context.BankAccounts
            join bnk in _context.Banks on ba.BankId equals bnk.ID
            join glB in _context.GLBalances.OrderByDescending(glb => glb.EndDate).Take(1)
              on ba.BankGL equals glB.GLAccountGuid 
            where glB.GLAccountGuid == ba.BankGL
            select new BankAccountDto()
            {
                BankId = ba.BankId,
                AccountNumber = ba.AccountNumber,
                BankName = bnk.BankName,
                Notes = ba.Notes,
                Description = ba.Description,
                Balance = (decimal)glB.ClosingBalance
            }).ToListAsync();

相关问题