【问题标题】:Converting SQL to LINQ将 SQL 转换为 LINQ
【发布时间】:2024-05-16 13:50:02
【问题描述】:

我有以下 SQL。我需要将其转换为 LINQ。

ALTER VIEW [dbo].[vwRptBorrowerAccount]  
AS  
SELECT dbo.tblAccount.[Creditor Registry ID], dbo.tblAccount.[Account No], dbo.tblAccount.[Date Opened], dbo.tblAccount.[Account Status ID],   
               dbo.tblAccount.[Date First Reported], dbo.tblAccount.[Credit Limit], dbo.tblAccount.Balance, dbo.tblAccount.[Minimum Installment], dbo.tblAccount.[Account Type],   
               dbo.tblAccount.Term, dbo.tblAccount.Purpose, dbo.tblAccount.[Account Owner Notes], dbo.tblAccount.[Creditor Notes], dbo.tblAccount.Collateral,   
               dbo.tblAccount.[Collateral Value], dbo.tblAccount.[Legal Status ID], dbo.tblAccount.[Legal Status Date], dbo.tblAccount.LastUpdatedBy,   
               dbo.tblAccount.LastUpdated, dbo.tblAccount.[Unique ID], dbo.tblAccount.[Account Status Date], dbo.tblAccount.Payment, dbo.tblAccount.[Payment Date],   
               dbo.tblAccount.[Balance Date], dbo.tblAccount.[Term Frequency], dbo.tblAccount.[State Change Date],   
               dbo.fn_GetAccountTypeDescription(dbo.tblAccount.[Account Type]) AS [Account Type Description], dbo.tblBusiness.[Business Name] AS CreditorName,   
               dbo.tblBusiness.Address AS CreditorAddress, dbo.tblBusiness.City AS CreditorCity, dbo.tblBusiness.State AS CreditorState,   
               dbo.tblLegalStatus.[Legal Status Description] AS [Legal Status], dbo.tblAccountStatus.[Account Status Description] AS [Account Status],   
               dbo.tblAccountOwner.[Account Owner Registry ID]  
FROM  dbo.tblAccount INNER JOIN  
               dbo.tblAccountOwner ON dbo.tblAccount.[Creditor Registry ID] = dbo.tblAccountOwner.[Creditor Registry ID] AND   
               dbo.tblAccount.[Account No] = dbo.tblAccountOwner.[Account No] INNER JOIN  
               dbo.tblBusiness ON dbo.tblAccount.[Creditor Registry ID] = dbo.tblBusiness.[Registry ID] INNER JOIN  
               dbo.tblAccountStatus ON dbo.tblAccount.[Account Status ID] = dbo.tblAccountStatus.[Account Status ID] INNER JOIN  
               dbo.tblLegalStatus ON dbo.tblAccount.[Legal Status ID] = dbo.tblLegalStatus.[Legal Status ID]  
WHERE (dbo.tblAccount.[Account Type] NOT IN ('CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04')) 

[已编辑] 功能细节是:

CREATE FUNCTION [fn_GetAccountTypeDescription]  
(  
 -- Add the parameters for the function here  
 @accountType varchar(max)  
)  
RETURNS varchar(max)  
with schemabinding  
AS  
BEGIN  
 -- Declare the return variable here  
 DECLARE @Result varchar(max)  

 -- Add the T-SQL statements to compute the return value here  
 IF EXISTS(SELECT Abbreviation FROM dbo.tblAccountType WHERE [Account Type Code] = @accountType)  
 BEGIN  
  SELECT @Result = Abbreviation FROM dbo.tblAccountType WHERE [Account Type Code] = @accountType  
 END  
 ELSE  
 BEGIN  
  SELECT @Result = @accountType  
 END  

 -- Return the result of the function  
 RETURN @Result  

END

您能否建议如何将其转换为 LINQ ?我不想使用连接。

【问题讨论】:

  • 我想使用关联。实体通过相关属性链接。
  • 尴尬的是fn_GetAccountTypeDescription
  • @Jodrell:我已更新问题以显示 fn_GetAccountTypeDescription
  • 您可以使用旨在执行此类操作的应用程序来帮助您入门,例如 linqpad 或 linqer...sqltolinq.com
  • 为什么要将其转换为 LINQ?你想从这个查询中创建什么对象?如果您只是想检索查询的结果,那么使用 ORM 是没有意义的——实际上这样做是个坏主意。顺便说一下LINQ to 什么? LINQ到SQL?实体?休眠?每个 ORM 都有自己的方式来指定应该同时加载某些实体,而不是分别加载每个实体。

标签: asp.net linq linq-to-entities


【解决方案1】:

无论是否使用 LINQ,我认为没有连接就无法做到这一点。

此外,这似乎是徒劳的练习。您希望从投资这方面获得什么好处?

此外,您还没有指定您打算使用的 LINQ 提供程序,因此您的问题完全无法回答(每个提供程序在语法上都有很大差异)。

【讨论】:

  • 我有一个在 asp.net 中设计的应用程序,我正在使用 LINQ 和实体框架 4.1 将其转换为 MVC3,这是其中的一部分。我没有表格中的数据,所以我无法验证我是否正确。
【解决方案2】:

好的,确保将 tblAccountType 添加到模型中,并且它与 tblAccount 有关联,然后执行如下操作。

我的测试能力比你还差,但我建议你有一个具有相同架构的测试数据库,并用一些虚拟数据填充它。

String[] excludedCodes = new String[]
    {
        "CA00",
        "CA01",
        "CA03",
        "CA04",
        "CA02",
        "PA00",
        "PA01",
        "PA02",
        "PA03",
        "PA04"
    };

var data = context.tblAccount.Where(a => !excludedCodes.Contains(a.AccountType))
    .Select(a => new{
            a.Creditor_Registry_ID,
            a.Account_No,
            a.Date_Opened,
            ...
            Account_Type_Description = a.tblAccountType.Where
                (t => t.Account_Type_Code = a.Account_Type).SingleOrDefault() 
                   ??  a.Account_Type),
            Creditor_Name = a.tblBusiness.Business_Name,
            CreditorAddress = a.tblBusiness.Address,
            ...
            Legal_Status = a.tblLegalStatus.Legal_Status_Description,
            ... etc.
        });

【讨论】: