asp.net - Linq query correction -
i want know if linq , sql equal ( means linq return same set of results sql ? dont have data in tables , need convert sql linq. please suggest
var excludetypes = new[] { "ca00", "ca01", "ca03", "ca04", "ca02", "pa00", "pa01", "pa02", "pa03", "pa04" }; var accounts = account in context.accounts owner in context.accountowners business in context.businesses accountstatus in context.accountstatuses legalstatus in context.legalstatuses !excludetypes.contains(account.accounttype) select new accountsreport { account = account }; alter view [dbo].[vwrptborroweraccount] 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]) [account type description], dbo.tblbusiness.[business name] creditorname, dbo.tblbusiness.address creditoraddress, dbo.tblbusiness.city creditorcity, dbo.tblbusiness.state creditorstate, dbo.tbllegalstatus.[legal status description] [legal status], dbo.tblaccountstatus.[account status description] [account status], dbo.tblaccountowner.[account owner registry id] dbo.tblaccount inner join dbo.tblaccountowner on dbo.tblaccount.[creditor registry id] = dbo.tblaccountowner.[creditor registry id] , 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] (dbo.tblaccount.[account type] not in ('ca00', 'ca01', 'ca03', 'ca04', 'ca02', 'pa00', 'pa01', 'pa02', 'pa03', 'pa04'))
no, linq not equivalent sql. missing relationships between tables.
the second , third "from" translated call system.linq.queryable.selectmany
. since there no relationship specified, query matches every account every accountowner , each result pair matched every business. known cartesian join (all possible matchings).
from account in context.accounts owner in context.accountowners business in context.businesses
a more traditional approach, specify relationship in query. query matches every account accountowner , every account matched business. known inner join. (note, must use keyword equals
. note strict scoping rules 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
the second , third "from" translated call system.linq.queryable.selectmany
. since there relationship specified, query matches every account accountowners , businesses. inner join (account = 1, others = many).
from account in context.accounts owner in account.accountowners business in account.businesses
this query matches every account accountowner , business.this inner join (account = many, others = 1).
from account in context.accounts let owner = account.accountowner let business = account.business
Comments
Post a Comment