C# 如何将select中带有子查询的SQL查询转换为.NET核心查询
我有下一个SQL本机查询:C# 如何将select中带有子查询的SQL查询转换为.NET核心查询,c#,mysql,.net-core,mariadb,C#,Mysql,.net Core,Mariadb,我有下一个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
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 core中的控制器中有以下查询:
[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);
}
}
[HttpGet]
公共行动结果GetAgentesByPlanta(字符串idPlanta)
{
使用(var db=new MyContext())
{
List lst=新列表();
变量q=
从数据库代理中的
在a.id_代理上的db.CustomData中加入cd等于cd.id_代理
其中((cd.id_字段==6)和(cd.description==idPlanta))
选择new{Agente=a,CustomData=cd};
foreach(q中的变量x)
{
一、新增(新增){
id\u agente=x.agente.id\u agente,
nombre=x.Agente.nombre,
direccion=x.Agente.direccion,
别名=x.Agente.alias,
ultimo\u contacto=x.Agente.ultimo\u contacto
});
}
动态响应=lst;
返回Ok(响应);
}
}
该控制器使用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
});
}
您可以在这里使用渴望加载修改@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()
};
这里再次我不知道时间戳是在哪个表+它是更优化,比目前的代码,但可以改善很多。。。使用ef或efcore的简单建议我将始终建议您使用可能需要system.Linq命名空间的存储库模式
如果对efcore不严格,您也可以使用sp,并通过efcore执行它,这对于像这样的查询更好,这不是一个好的解决方案。ORM的工作是从实体之间的关系生成连接。您应该为实体添加适当的关系