簡體   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