我有下一个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;
我在这样的控制器的.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); } }
该控制器以json响应,并且可以正常工作。 但是如您所见,select的子查询丢失了。
¿如何在此.NET Core查询中添加子查询?