2011-05-11 9 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')) 

[EDITED] 과 기능의 세부 사항은 다음과 같습니다

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

+0

어색한 비트는'fn_GetAccountTypeDescription' – Jodrell

+0

@Jodrell 될 것입니다 : 난 당신이 얻을 이런 종류의 작업을 수행하도록 설계 응용 프로그램을 사용할 수 – DotnetSparrow

답변

0

LINQ 사용 여부와 상관없이 조인없이이 작업을 수행 할 수 있다고 생각하지 않습니다.

또한 무의미한 운동처럼 보입니다. 당신이 투자 할 때 얻게 될 이익은 무엇입니까?

또한 LINQ Provider를 사용할 것인지 지정하지 않았으므로 질문에 완전히 답할 수 없습니다 (각 공급자마다 구문에 큰 차이가 있음).

+0

asp.net에서 설계된 응용 프로그램이 있고 LINQ 및 Entities Framework 4.1을 사용하여 MVC3으로 변환 중입니다.이 부분은 그 것입니다. 테이블에 데이터가 없기 때문에 제대로 작동하는지 확인할 수 없습니다. – DotnetSparrow

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. 
     });