简体   繁体   English

实体框架核心:如何通过仅选择最新创建的记录在“ IQueryable”上执行联接?

[英]Entity Framework Core: How to perform a Join on `IQueryable` by selecting only the latest created records?

I am refactoring a code in production which create duplicates: 我正在重构生产中重复的代码:

private IQueryable<CashoutModel> GetCashoutModels() =>
    from cashout in _context.Cashouts
    join audit in _context.Audits
        on cashout.Id.ToString() equals audit.EntityId
        into cashoutModel
    from audit in cashoutModel.DefaultIfEmpty()
    orderby cashout.CreatedOn descending
    select new CashoutModel
    {
        Id = cashout.Id,
        Amount = cashout.Amount,
        Comment = cashout.Comment,
        CreatedOn = cashout.CreatedOn,
        RecipientAccountId = cashout.RecipientAccountId,
        RecipientAccountName = cashout.RecipientAccountName,
        State = cashout.State,
        Reason = cashout.Reason,
        CreatedBy = audit == null 
            ? null 
            : audit.Name
    };

The _context.Audits actually records (use-triggered) changes that happen for certain records in _context.Cashout . _context.Audits实际上在_context.Cashout记录了某些记录发生的(使用触发)更改。

I am looking for a clean way to fix the current behavior of the join by selecting only (for a given EntityId ) the latest audit record. 我要寻找一个干净的方式来解决的当前行为join只选择(对于给定EntityId )最新的审计记录。

The code you currently have is essentially doing a LEFT OUTER JOIN which gets all the Cashouts records and the corresponding audit record. 您当前拥有的代码实际上是在做一个LEFT OUTER JOIN ,它获取所有Cashouts记录和相应的审计记录。

Assuming you have a timestamp column (say CreatedOn ) in the Audits table which captures when that audit record was created, you could use a GROUP JOIN here like so - 假设您在“ Audits表中有一个时间戳列(例如CreatedOn )捕获创建该审计记录的时间,则可以在此处使用GROUP JOIN ,如下所示:

private IQueryable<CashoutModel> GetCashoutModels() =>
_context.Cashouts.GroupJoin(_context.Audits,
                            c => c.Id.ToString(),
                            a => a.EntityId,
                            (cashout, audit) => new CashoutModel
                                {
                                    Id = cashout.Id,
                                    Amount = cashout.Amount,
                                    Comment = cashout.Comment,
                                    CreatedOn = cashout.CreatedOn,
                                    RecipientAccountId = cashout.RecipientAccountId,
                                    RecipientAccountName = cashout.RecipientAccountName,
                                    State = cashout.State,
                                    Reason = cashout.Reason,
                                    CreatedBy = audit.OrderByDescending(x => x.CreatedOn)
                                                    .Select(y => y.Name)
                                                    .FirstOrDefault()
                                }).AsQueryable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体框架 6 中编写 IQueryable 连接 - How to write IQueryable join in Entity Framework 6 如何使用C#.net核心实体框架执行联接 - How to perform a join in c# .net core Entity Framework using 如何在 .NET Core 3.0 实体框架中执行组加入? - How to perform a group join in .NET Core 3.0 Entity Framework? EF ID为IQueryable的EF Core连接实体IQueryable - EF Core join entity IQueryable with id IQueryable 使用 Entity Framework Core 5,如何嵌套查询并且仍然有 IQueryable? - With Entity Framework Core 5, how to nest a query and still have an IQueryable? 如何删除 Entity Framework Core 中指定日期之前的记录,不包括最新的? - How do I delete records before a specified date in Entity Framework Core excluding the latest? 实体框架返回两个表的联接的IQueryable - Entity Framework returning IQueryable of join of two tables 实体框架核心未添加记录 - Entity Framework Core not adding records 如何使用LINQ在实体框架中执行这种类型的JOIN - How can I perform this type of JOIN in Entity framework with LINQ 如何在 iqueryable 中强制转换派生 BaseModel 具有 isdelete 标志,在实体框架核心中制作软删除存储库 - how to cast derived BaseModel have isdelete flag in iqueryable, make soft delete repository in entity framework core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM