c# - How to write fast subquery in where condition in linq -


I try to type the subquery in the linq where the condition. But when I look at my SQL syntax on SQL Server Profiler, my query looks complex and long and it takes a timeout error in the server.

I want to write a query below. Select T1.Id, as [MaxHour] from T2.Id, DATEADD (Hour, IsNull ((Select Max (T3.MaxHour) with TABLE3 T3 (NOLOCK) Where T1.CategoryId = T3.CategoryId), 0), T1 .EndDate, T1.StartDate Table1 with T1 (NOLOCK) T1.Id = T2 on table 2 T2 (NOLOCK) connected to outer space. Id WHERE DATEADD (HOURS, ISNULL (Select Max (T3.MaxHour) from [MaxHour] with TABLE3 T3 (NOLOCK) where T1.CategoryId = T3.CategoryId, 0), T1.EndDate) & gt; '2014-11-05 00:00:00' and T1. Start date & lt; '2014-11-05 06:00:00'

I have written my Linux expression like this

  on actualData = from T1 to _context.TABLE1 In T1.Id, it is equivalent to T2.Id in data from X. in Data.DefaultIfEmpty joining T2 in _context.TABLE2 () where EntityFunctions.AddHours (T1. And Diet, (? Int?) ((T3 In T-Exact. TABLE3 where T3.CategoryId == T1.CategoryId's selection T3) .Max (line =>; row.MaxHour))) ?? 0) & gt; = '2014- 11-05 00:00:00' & amp; Amp; T1.StartDate & lt; '2014-11-05 06:00:00' to select new {ID1 = T1.Id ID2 = T2.Id, StartDate = T1.StartDate on ENDDATE = EntityFunctions.AddHours (T1.EndDate, ((Integer?) ( (From T3 to _context.TABLE3 where T3.CategoryId == T1.CategoryId's selection T3) .max (line = & gt; row.maxHour))) ?? 0)};  

But it does not seem that I want to see in SQL

How can I write in linq?

Thank you. Try

:

  at actualData = _context.TABLE1 T1 to T1.Id is included in T.X. in T. 2 in T. Data.DefaultIfEmpty equals T2.Id in data from X () go endate = EntityFunctions.AddHours (T1.EndDate, ((T3 to _context. In TABLE3 where T3.CategoryId == T1.CategoryId Selection T3) .max (line = & gt; row.MaxHour))) ?? 0) where endidet & gt; = '2014-11-05 00:00:00' & amp; Amp; T1.StartDate & lt; '2014-11-05 06:00:00' select new {Id1 = T1.Id, Id2 = T2.Id, StartDate = T1.StartDate, EndDate = enddate};  

And I suspect you can leave Dost at (int?) , because is the maximum (int) default return By int?


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -