2011-05-11 114 views
1

我想知道Linq和SQL是否相等(意味着Linq會返回與SQL相同的結果集嗎?我沒有表中的數據,並且需要將SQL轉換爲LINQ請建議Linq查詢糾正

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

var accounts = 
       from account in context.Accounts 
       from owner in context.AccountOwners 
       from business in context.Businesses 
       from accountStatus in context.AccountStatuses 
       from legalStatus in context.LegalStatuses 
       where !excludeTypes.Contains(account.AccountType) 
       select new AccountsReport { Account = account }; 


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')) 

回答

3

不,你的LINQ不等同於您的SQL語句。你缺少的表之間的關係。


第二個和第三個「從」被翻譯成呼叫System.Linq.Queryable.SelectMany。由於沒有關係指定,這個查詢匹配每個帳戶到每個AccountOwner然後每個結果對都與每個業務相匹配。這被稱爲笛卡爾連接(所有可能的匹配)。

from account in context.Accounts 
from owner in context.AccountOwners 
from business in context.Businesses 

更傳統的做法是在查詢中指定關係。此查詢將每個帳戶與其AccountOwner匹配,然後每個帳戶都與其業務相匹配。這被稱爲內連接。 (請注意,必須使用關鍵字equals。同時請注意嚴格的範圍規則on (leftside) equals (rightside))。

from account in context.Accounts 
join owner in context.AccountOwners 
    on new {account.RegistryId, account.AccountNo} 
    equals new {owner.RegistryId, owner.AccountNo} 
join business in context.Businesses 
    on account.CreditorRegistryID 
    equals business.RegistryID 

第二個和第三個「from」被翻譯成對System.Linq.Queryable.SelectMany的調用。由於存在指定的關係,因此此查詢會將每個帳戶與其AccountOwner及其業務相匹配。這是一個內部連接(Account = 1,others = Many)。

from account in context.Accounts 
from owner in account.AccountOwners 
from business in account.Businesses 

這個查詢每個帳戶相匹配,其唯一AccountOwner其唯一Business.This也是內部連接(帳戶=許多,人= 1)。

from account in context.Accounts 
let owner = account.AccountOwner 
let business = account.Business 
+0

您能否建議如何改正?我已經定義了實體之間的關係。 – DotnetSparrow 2011-05-11 17:02:34

+0

如果我這樣寫:從帳戶的上下文。帳戶 來自業主帳戶。帳戶所有者 從業主業務。企業 – DotnetSparrow 2011-05-12 06:14:08

+0

當然,這將工作。 – 2011-05-12 12:11:45