In one sentence, convert a Linq statement to an SQL statement.

Source: Internet
Author: User

In one sentence, convert a Linq statement to an SQL statement.
Zookeeper

Public IEnumerable <VMB_Report> ReportView_List (VMB_ReportConditions requiredModel)
{

IEnumerable <VMB_Report> resultModel = new List <VMB_Report> ();
IQueryable <Merchant> merchantList;
MerchantList = Report_List ();

# Region obtain the VMB_Report set from MerchantList


ResultModel = merchantList. Select (o => new VMB_Report
{

MerchantID = o. MerchantID,
MID = o. MID,
DBA = o. DBA,
DBAPhone = o. DBAPhone,
ContactName = o. ContactName,
TotalSalesYTD = o. MerchantVolume. TotalSalesYTD,
BankcardAvgTicketYTD = o. MerchantVolume. BankcardAvgTicketYTD,
YTDProfit = o. MerchantVolume. YTDProfit,
EnteredDate = o. MerchantStatus. EnteredDate,
InstalledDate = o. MerchantStatus. InstalledDate,
Status = o. MerchantStatus. Status,
RegionName = o. MerchantReps. Where (a => a. LevelNumber = 1). FirstOrDefault (). Rep. Region. RegionName,
RepLev1 = (o. MerchantReps. Any (a => a. LevelNumber = 1 )? O. MerchantReps. Where (a => a. LevelNumber = 1). FirstOrDefault (). Rep. Name :""),
RepLev2 = (o. MerchantReps. Any (a => a. LevelNumber = 2 )? O. MerchantReps. Where (a => a. LevelNumber = 2). FirstOrDefault (). Rep. Name :""),
RepLev3 = (o. MerchantReps. Any (a => a. LevelNumber = 3 )? O. MerchantReps. Where (a => a. LevelNumber = 3). FirstOrDefault (). Rep. Name :""),
RepLev4 = (o. MerchantReps. Any (a => a. LevelNumber = 4 )? O. MerchantReps. Where (a => a. LevelNumber = 4). FirstOrDefault (). Rep. Name :""),
RepLev5 = (o. MerchantReps. Any (a => a. LevelNumber = 5 )? O. MerchantReps. Where (a => a. LevelNumber = 5). FirstOrDefault (). Rep. Name :""),
Address1 = o. DBAAddress1,
Address2 = o. DBAAddress2,
City = o. DBACity,
State = o. DBAState,
Zip = o. DBAZip,
Ps = (from op in o. Products
Where op. Status = 100
Select new VMPro
{
EFDe = op. equipment.pdf. Description,
Ptype = op. ProductType,
SDe = op. Software. Description
}). ToList ()

});
# Endregion

Var SQL = (resultModel as ObjectQuery <VMB_Report>). ToTraceString (); // This sentence is the essence. Viewing SQL variables is the generated SQL statement.

Return resultModel;
}


Convert an SQL statement to a Linq statement

Points less! Help my team back to work and pay off! O (∩) O Haha ~
-------------
My tables are Table_1s and Table_2s.
The following figure shows the linq query result:
From t in Table_1s
Join f in Table_2s on t. B equals f. Key into FD
From f in FD. Where (B => B. RowName = "B"). DefaultIfEmpty ()
Join s in Table_2s on t. C equals s. Key into FDS
From s in FDS. Where (B => B. RowName = "C"). DefaultIfEmpty ()
Select new
{
A = t.,
B = f. Value,
C = s. Value
}
---
The corresponding SQL statement is as follows:
-- Region Parameters
DECLARE @ p0 NVarChar (1) SET @ p0 = 'B'
DECLARE @ p1 NVarChar (1) SET @ p1 = 'C'
-- EndRegion
SELECT [t0]. [A], [t1]. [value] AS [B], [t2]. [value] AS [C]
FROM [Table_1] AS [t0]
Left outer join [Table_2] AS [t1] ON ([t1]. [RowName] = @ p0) AND ([t0]. [B] = [t1]. [key])
Left outer join [Table_2] AS [t2] ON ([t2]. [RowName] = @ p1) AND ([t0]. [C] = [t2]. [key])
---
Should meet your requirements!

How to convert SQL statements into LINQ statements:

Var q = from t in table
Group t by t.doc ument into g
Select new {
G. Key,
P1 = g. Max (p => p. P1 ),
P2 = g. Max (p => p. P2)
}
// Hand-written, please check in the tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.