I want to know if the Linq and SQL are equal ( means Linq will return same set of results as the SQL ? I dont have data in tables and I need to convert SQL to LINQ. Please suggest
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'))
No, your linq is not equivalent to your sql. You are missing the relationships between the tables.
The second and third “from” is translated into a call to
System.Linq.Queryable.SelectMany. Since there is no relationship specified, this query matches every Account to every AccountOwner and then each result pair is matched to every Business. This is known as cartesian join (all possible matchings).A more traditional approach, is to specify the relationship in the query. This query matches every Account to its AccountOwner and then every Account is matched to its Business. This is known as inner join. (Note, must use keyword
equals. Also note strict scoping ruleson (leftside) equals (rightside)).The second and third “from” is translated into a call to
System.Linq.Queryable.SelectMany. Since there is a relationship specified, this query matches every Account to its AccountOwners and to its Businesses. This is an inner join (Account = 1, others = Many).This query matches every Account to its only AccountOwner and its only Business.This is also an inner join (Account = Many, others = 1).