在 GroupJoin 中使用 ToList 导致“无法翻译”错误

Using ToList in GroupJoin cause "could not be translated" error

提问人:GasTorrent 提问时间:11/10/2023 最后编辑:Svyatoslav DanylivGasTorrent 更新时间:11/11/2023 访问量:51

问:

我正在尝试加入所有符合条件的回复 Replies.ReplyTo == Ticket.Id 在 reportInfo 对象中。但是在执行请求时:

错误信息:

System.InvalidOperationException: The LINQ expression 'DbSet<HeskTicket>()
.Where(x => x.Status.Equals(3) && x.Custom7.Equals("Create") && x.FileCheck.Equals(False) && x.OrderDate < x.LastChange)
.Join(
inner: DbSet<HeskCategory>(),
outerKeySelector: ticket => ticket.Category,
innerKeySelector: category => category.Id,
resultSelector: (ticket, category) => new ReportInfo{
Ticket = ticket,
Category = category
}
)
.Join(
inner: DbSet<HeskUser>(),
outerKeySelector: ticket => ticket.Ticket.Owner,
innerKeySelector: user => user.Id,
resultSelector: (ticket, user) => new ReportInfo{
Ticket = ticket.Ticket,
Category = ticket.Category,
User = user
}
)
.LeftJoin(
inner: DbSet<HeskTemplateRelation>()
.Include(dr => dr.Placeholders)
.AsQueryable(),
outerKeySelector: ticket => ticket.Ticket.Custom6,
innerKeySelector: dataRelation => dataRelation.ActName,
resultSelector: (l, namelessParameter{0}) => new ReportInfo{
Ticket = l.Ticket,
Category = l.Category,
User = l.User,
TemplateRelation = namelessParameter{0}
}
)
.GroupJoin(
inner: DbSet<HeskReply>(),
outerKeySelector: ticket => ticket.Ticket.Id,
innerKeySelector: replies => replies.Replyto,
resultSelector: (ticket, replies) => new ReportInfo{
Ticket = ticket.Ticket,
Category = ticket.Category,
User = ticket.User,
TemplateRelation = ticket.TemplateRelation,
Replies = replies
.ToList()
})

无法翻译。要么以可以 被翻译,或通过插入 调用“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或 “ToListAsync”。请参见 https://go.microsoft.c om/fwlink/?linkid=2101038 了解更多信息。

报告信息

public class ReportInfo
    {
        public HeskTicket Ticket { get; set; }
        public HeskCategory Category { get; set; }
        public HeskTemplateRelation TemplateRelation { get; set; }
        public HeskUser User { get; set; }
        public List<HeskReply> Replies { get; set; }
    }

这是我的 Join 表达式:

 public async Task<List<ReportInfo>> GetInfoForReport(CancellationToken cancellationToken)
        {
            var joinedData = await _mariaDbContext.HeskTickets.Where(x =>
                    x.Status.Equals(SearchConstants.Closed) &&
                    x.Custom7.Equals(SearchConstants.Required) &&
                    x.FileCheck.Equals(false) &&
                    x.OrderDate < x.LastChange)
                .Join(
                    _mariaDbContext.HeskCategories,
                    ticket => ticket.Category,
                    category => category.Id,
                    (ticket, category) => new ReportInfo()
                    {
                        Ticket = ticket,
                        Category = category
                    })
                .Join(
                    _mariaDbContext.HeskUsers,
                    ticket => ticket.Ticket.Owner,
                    user => user.Id,
                    (ticket, user) => new ReportInfo()
                    {
                        Ticket = ticket.Ticket,
                        Category = ticket.Category,
                        User = user
                    })
                .LeftJoin(_mariaDbContext.HeskTemplateRelation.Include(dr => dr.Placeholders),
                    ticket => ticket.Ticket.Custom6,
                    dataRelation => dataRelation.ActName,
                    (ticket, dataRelation) => new ReportInfo()
                    {
                        Ticket = ticket.Ticket,
                        Category = ticket.Category,
                        User = ticket.User,
                        TemplateRelation = dataRelation
                    })
                .GroupJoin(
                    _mariaDbContext.HeskReplies,
                ticket => ticket.Ticket.Id,
                    replies => replies.Replyto,
                    (ticket, replies) => new ReportInfo()
                        {
                            Ticket = ticket.Ticket,
                            Category = ticket.Category,
                            User = ticket.User,
                            TemplateRelation = ticket.TemplateRelation,
                            Replies = replies.ToList()
                        }
                    )
                .ToListAsync(cancellationToken);

            return joinedData;
        }

GroupJoin 之前的所有代码都正常工作。

Expectig reportInfo 对象,其中包含与票证 ID 相等的回复列表。我尝试加入,但它导致创建许多包含 1 个回复而不是回复列表的副本。

C# asp.net LINQ 联接 实体框架核心

评论

0赞 GasTorrent 11/10/2023
sry,翻译改变了一些多余的东西:D
0赞 Selvin 11/10/2023
为什么你认为 EF 使用 ToList() 翻译 GroupJoin?...请参阅官方文档的复杂查询运算符主题
0赞 Svyatoslav Danyliv 11/11/2023
你试过查询语法吗?我不明白你想检索什么。 也是未知的扩展名。LeftJoin

答: 暂无答案