在 EF Core 查询中使用 let 关键字发出问题

问题描述 投票:0回答:1

我想在 C# 中使用 EF Core 7 执行此查询:

var query = (from t in _dbContext.Domaines
                             join tick1 in _dbContext.Tickets on t.Iddomaine equals tick1.Iddomaine
                             join etatticket in _dbContext.Etattickets on tick1.Idticket equals etatticket.Idticket
                             join etat in _dbContext.Etats on etatticket.Idetat equals etat.Idetat
                             join contact in _dbContext.Contacts on tick1.Idcontact equals contact.Idcontact
                             group new { t, tick1, etatticket, etat, contact } by new { t.Iddomaine, tick1.Idticket, contact.Nom, contact.Prenom } into g
                             let LibelleCurrentEtatTicket = (from e in _dbContext.Etattickets
                                                             join et in _dbContext.Etats on e.Idetat equals et.Idetat
                                                             where g.Key.Idticket == e.Idticket
                                                             group e by new { et.Intituleetat, e.Dateaction } into g2
                                                             let maxDateAction = g2.Max(x => x.Dateaction)
                                                             where g2.Key.Dateaction == maxDateAction
                                                             select g2.Key.Intituleetat).ToList()
                             select new TicketSpecifique
                             {
                                 
                                 Idticket = g.Key.Idticket,
                                 
                                 Intitule = g.FirstOrDefault().tick1.Intitule,
                                 Iddomaine = g.Key.Iddomaine,
                                 IntituleDomaine = g.FirstOrDefault().t.Intituledomaine,
                                 IntituleEtatTicket = LibelleCurrentEtatTicket.FirstOrDefault(),
                                 NomContact = g.Key.Nom,
                                 PrenomContact = g.Key.Prenom,
                                 Suiviinterne = g.FirstOrDefault().tick1.Suiviinterne,
                                 Suiviclient = g.FirstOrDefault().tick1.Suiviclient,
                                 
                             }).OrderBy(x => x.Idticket);

当我想使用

query.ToList()
时,我遇到以下问题:

我尝试在互联网上搜索,有些主题谈到了 EF 中的一个错误。这是真的吗?有人回答过这个问题吗?

c# database entity-framework
1个回答
0
投票

在您的情况下,问题是由于在查询中使用 let 和 ToList() 引起的,这有时会导致查询无法直接转换为 SQL。

要解决此问题,您可以修改查询以避免使用 let 和 ToList()。相反,您可以在主查询中计算 LibelleCurrentEtatTicket 值。

这是您的查询的修改版本:

var query = (
    from t in _dbContext.Domaines
    join tick1 in _dbContext.Tickets on t.Iddomaine equals tick1.Iddomaine
    join etatticket in _dbContext.Etattickets on tick1.Idticket equals etatticket.Idticket
    join etat in _dbContext.Etats on etatticket.Idetat equals etat.Idetat
    join contact in _dbContext.Contacts on tick1.Idcontact equals contact.Idcontact
    group new { t, tick1, etatticket, etat, contact } by new { t.Iddomaine, tick1.Idticket, contact.Nom, contact.Prenom } into g
    select new TicketSpecifique
    {
        Idticket = g.Key.Idticket,
        Intitule = g.FirstOrDefault().tick1.Intitule,
        Iddomaine = g.Key.Iddomaine,
        IntituleDomaine = g.FirstOrDefault().t.Intituledomaine,
        IntituleEtatTicket = g.OrderBy(e => e.etatticket.Dateaction)
                                .LastOrDefault()
                                .etat.Intituleetat,
        NomContact = g.Key.Nom,
        PrenomContact = g.Key.Prenom,
        Suiviinterne = g.FirstOrDefault().tick1.Suiviinterne,
        Suiviclient = g.FirstOrDefault().tick1.Suiviclient,
    })
    .OrderBy(x => x.Idticket);

var result = query.ToList();

© www.soinside.com 2019 - 2024. All rights reserved.