簡體   English   中英

如何將 SQL 查詢與 select 中的子查詢轉換為 .NET 核心查詢

[英]How to translate a SQL query with subquery in select to .NET Core query

我有下一個 SQL 原生查詢:

select 
    a.id_agente,
    a.alias,
    a.direccion,
    cd.description, 
    (   select te.data 
        from tevento te 
        left join tagente ta on ta.id_agente = te.id_agente 
        where ta.id_agente = a.id_agente order by timestamp desc limit 1
    ) as data
from tagente a 
left join tagent_custom_data cd on a.id_agente = cd.id_agent 
where cd.id_field = 6 and cd.description = $VAR;

我在 controller 的 .net 核心中有這個查詢,如下所示:

    [HttpGet]
    public ActionResult<string> GetAgentesByPlanta(string idPlanta)
    {
        using (var db = new MyContext())
        {
            List<Object> lst = new List<Object>();

            var q =
                from a in db.Agente
                join cd in db.CustomData on a.id_agente equals cd.id_agent 
                where ((cd.id_field == 6) & (cd.description == idPlanta))
                select new { Agente = a, CustomData = cd };

            foreach (var x in q)
            {
                lst.Add(new {
                    id_agente=x.Agente.id_agente,
                    nombre=x.Agente.nombre,
                    direccion=x.Agente.direccion,
                    alias=x.Agente.alias,
                    ultimo_contacto=x.Agente.ultimo_contacto
                });
            }

            dynamic response = lst;

        return Ok(response);
        }
    }

這個 controller 以 json 響應,它的工作原理。 但如您所見,select 的子查詢缺失。

¿如何在此 .NET 核心查詢中添加子查詢?

我終於以這種方式解決了這個疑問:

             var q =
                from a in db.Agente
                join cd in db.CustomData on a.id_agente equals cd.id_agent 
                where ((cd.id_field == 6) && (cd.description == idConvert))
                select new { 
                    Agente = a, 
                    CustomData = cd,
                    Evento = (from te in db.Evento
                            join ta in db.Agente on te.id_agente equals ta.id_agente
                            where ta.id_agente == a.id_agente
                            orderby te.timestamp descending
                            select new {Evento = te}).First()
                };

            foreach (var x in q)
            {
                lst.Add(new {
                    id_agente=x.Agente.id_agente,
                    nombre=x.Agente.nombre,
                    direccion=x.Agente.direccion,
                    alias=x.Agente.alias,
                    ultimo_contacto=x.Agente.ultimo_contacto,
                    data=x.Evento.Evento.data,
                    ultimo_data=x.Evento.Evento.timestamp
                });
            }

您可以在此處使用 Eager Loading 修改@baquilare 答案以獲取示例

 var q =   from a in db.Agente
                join cd in db.CustomData on a.id_agente equals cd.id_agent 
                join te in db.Evento.AsQueryable().Include(x=>x.Agente) on
                  te.Agente.id_agente == a.id_agente
                where ((cd.id_field == 6) && (cd.description == idConvert))
                select new { 
                             Agente = a, 
                             CustomData = cd,
                             Evento =te.OrderByDescending(x=>x.timestamp).FirstOrDefault()
                };

在這里,我再次不知道時間戳在哪個表中+它比當前代碼更優化,但可以改進很多......命名空間也是

如果對 efcore 不嚴格,您也可以使用 sp 並通過 efcore 執行它,這對於像這樣的查詢更好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM