繁体   English   中英

在ef core 2.2中执行'select * from(select * from…)'查询

[英]Perform 'select * from (select * from …)' query in ef core 2.2

我正在尝试使用实体框架核心2.2在c#中使用与此(SQL)的子查询执行类似的查询

select ST_LengthSpheroid(ST_MakeLine(a."Location"),'SPHEROID["WGS 84",6378137,298.257223563]') AS Length
from (select * from "Logs" where "CarId" = 191
      order by "Id") as a;

在实体框架> 2.0中,我尝试执行左联接,但是使用自定义功能时,遇到了EF核心警告或错误。 是否有任何适当的方法来实现该查询?

这样的事情行吗?

select ST_LengthSpheroid(ST_MakeLine(TrackerLogs.Location),'SPHEROID["WGS 84",6378137,298.257223563]') AS Length
from TrackerLogs
where CarId = 191
order by Id

好的,也许有人知道更好的解决方案,但是经过一些研究,我无法使用 ef内核来实现这一点,因此我使用了FromSql

var queryable = qLogs.FromSql("select * from \"" + tableName + "\" order by \"" + orderField + "\"");

var result = qCars.Select(x => new RouteModel
{
   Mileage = Math.Round(
       queryable
           .Where(y => y.CarId == x.Id)
           .Select(y => PostgisExtensions.ST_LengthSpheroid(
                    PostgisExtensions.ST_MakeLine(
                        PostgisExtensions.ST_GeomFromText(y.Location.AsText(),          PostgisConstants.MetricSrid)
                    ),
                   PostgisConstants.SpheroidWgs84)
           )
           .FirstOrDefault() / 1000),
 .....

所以,现在我可以orderby使用子查询没有groupbydistinct on 完美运行,没有任何ef核心警告,并生成预期的查询:

  SELECT ROUND(COALESCE((
      SELECT ST_LengthSpheroid(ST_MakeLine(ST_GeomFromText(ST_AsText(x0."Location"), 4326)), 'SPHEROID["WGS 84",6378137,298.257223563]')
      FROM (
          select * from "Logs" order by "FixedAt"
      ) AS x0
      WHERE (x0."CarId" = x."Id")
      LIMIT 1
  ), 0.0) / 1000.0) AS "Mileage", 
  .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM