2016-12-16 6 views
0

linqクエリをSQL Serverクエリに変換するときに問題が発生しました。Entity Frameworkクエリをjoinおよびgroup by SQL Serverクエリに変換する

var gdevices = (from logs in dbContext.GensetLogs 
       group logs by logs.DeviceId into logsgroup 
       join devices in dbContext.GensetDevices on logsgroup.FirstOrDefault().DeviceId equals devices.Id 
       where devices.RegisteredBy == model.Email || devices.OperatedBy == model.Email || model.StType == "admin" 
       select new DeviceRegistrationDTO 
          { 
           PhoneNumber = devices.PhoneNumber, 
           Latitude = devices.Latitude, 
           Longitude = devices.Longitude, 
           LatestRT = logsgroup.Max(d => d.ReadingTime), 
           DeviceName = logsgroup.Max(d => d.ReadingTime).DeviceName, 
           OperatedBy = devices.OperatedBy, 
           ThresholdValue = devices.ThresholdValue 
          }).ToList(); 
+3

プレーンSQLに変換しますか?うーん、なぜあなたはそれをやりたいのか分からないが、 'ToList'を' ToString'に置き換えるとあなたのSQLクエリがある。 –

+0

[Entity Frameworkによって生成されたSQLを表示するにはどうすればよいですか?](http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity -framework) – Igor

答えて

0

私は非常に単純な方法でこれを行って。

SELECT (t.[TransId]) 
    , t.[SGCode] 
    , t.PurchaseDate 
    , t.SoldTo 
     ,t.Cost 
    ,(select top 1 dr.Rate from DepreciationRate dr where t.Assets_TransId = dr.Assets_TransId order by dr.DepDate desc) Rate 
    ,(select sc.Name from AssetsSubClass sc where t.SubClass_TransId = sc.TransId) Name 

FROM AssetsTransctions t 
関連する問題