0

I having problems converting linq query to a SQL Server query.

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();
Saif
  • 394
  • 3
  • 13
  • 3
    Convert to plain SQL? Hmm, not sure why would you want to do that, but replace `ToList` with `ToString` and there is your SQL query. – Ivan Stoev Dec 16 '16 at 12:44
  • Possible duplicate of [How do I view the SQL generated by the Entity Framework?](http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework) – Igor Dec 16 '16 at 12:55

2 Answers2

0

If you are trying to convert from Linq to sql queries then use as following

var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();

Ref:

https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

Meer
  • 656
  • 9
  • 18
0

i done this in very simple way.

    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
Saif
  • 394
  • 3
  • 13