【问题标题】:Convert SQL query/subqueries to LINQ将 SQL 查询/子查询转换为 LINQ
【发布时间】:2014-01-21 10:25:07
【问题描述】:

原文:

select *, (PlanBasedOnDataUseage - CurrentPlanPricePerMonth) as MonthlyOverageInvoiceAmount from       

(      
Select *  ,       

(select top 1 PricePerMonth from accounttypes where IsDeleted = 0 and AvailableOnline = 1      
and  RecordsPerMonth > 0) as PlanBasedOnDataUseage      
 ,       
(select top 1 AccountTypeName from accounttypes where IsDeleted = 0 and AvailableOnline = 1      
and  RecordsPerMonth > 0) as SuggestPlanBasedOnDataUsage      
from      
(      
select       
 ClientID, ClientName, ClientEmail, AccountOwnerName,       
 AccountExpiryDate,       
 DATEADD(day, DAY(AccountExpiryDate)-1,       
 CAST(      
  cast(YEar(GETDATE())as nvarchar(4)) +       
  replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'      
   as datetime)      
 ) as MonthlyRollOverDate      
 , AT.RecordsPerMonth as CurrentPlanMentionsMaxPerMonth      
    , AT.AccountTypeName as CurrentPlanAccountType       
 , AT.PricePerMonth as CurrentPlanPricePerMonth      
 from clients C      
 inner join AccountTypes AT on C.AccountTypeID = AT.AccountTypeID       
 LEFT OUTER JOIN AccountOwner AO on AO.AccountOwnerID = C.AccountOwnerID  
 WHERE       
 AT.AccountTypeName not in ('Standard', 'Demo', 'Trial') and      
 AccountExpiryDate > getdate()      
  AND      
  (      
    DATEADD(day, DAY(AccountExpiryDate)-1,       
     CAST(      
      cast(YEar(GETDATE())as nvarchar(4)) +       
      replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'      
       as datetime)      
     )       

   >=        
    --Monday Last Week      
    dateadd(d, -7, DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0))       
  AND      
    --Calc MonthlyRollOverDate      
      DATEADD(day, DAY(AccountExpiryDate)-1,       
       CAST(      
        cast(YEar(GETDATE())as nvarchar(4)) +       
        replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'      
         as datetime)      
       )       
   <=       
    -- THis Monthday      
    DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)      



  )       
) as ClientsRollingOverThisWeek      

) as ClientsRollingOverThisWeekWithRealPlanAttached       

where (PlanBasedOnDataUseage - CurrentPlanPricePerMonth) > 0    

【问题讨论】:

标签: c# asp.net sql sql-server linq


【解决方案1】:

您可以使用Linqpad 显示 LINQ 查询的 SQL 输出。

【讨论】:

  • @Sourav 是正确的。 Linqpad 不会将 SQL 转换为 LINQ。但是,它确实显示了 LINQ 查询的 SQL 输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
相关资源
最近更新 更多