2011-05-11 134 views
0

我有以下SQL。我需要將其轉換爲LINQ。將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?我不想使用連接。

+0

我想使用關聯。實體通過相關屬性進行鏈接。 – DotnetSparrow 2011-05-11 17:12:04

+0

尷尬的位將是'fn_GetAccountTypeDescription' – Jodrell 2011-05-11 17:13:06

+0

@Jodrell:我已更新問題以顯示fn_GetAccountTypeDescription – DotnetSparrow 2011-05-11 17:17:14

回答

0

我不認爲你將能夠做到這一點沒有連接,無論是否使用LINQ。

此外,它似乎相當於無用的練習。你從這次投資中獲得什麼收益?

此外,您尚未指定您打算使用的LINQ提供程序,因此您的問題完全無法解析(每個提供程序在語法上存在顯着差異)。

+0

我有一個在asp.net中設計的應用程序,我將它轉換爲使用LINQ和實體框架4.1的MVC3,這是其中的一部分。我沒有表中的數據,所以我不能驗證如果我做得對或不。 – DotnetSparrow 2011-05-11 17:26:33

0

好的,請確保您將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. 
     });