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

Perform 'select * from (select * from ...)' query in ef core 2.2

我正在尝试使用 entity framework core 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;

在 entity framework > 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

好吧,也许有人知道更好的解决方案,但经过一些研究后,我无法使用 pure ef core 实现,所以我使用了 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", 
  .....