Learn to construct complex lambda puzzles use linqpad and linqer to achieve mutual transfer between SQL and lambda

Source: Internet
Author: User
Tags what sql

One: We may encounter two problems with LINQ:

1. When our LINQ performance is low, what if the optimization????

The SQL that we write for LINQ generates no control ... (To do performance optimization, you must know in advance what SQL will generate??? )

SQL profile view generated SQL ...


At this point, we have to have a tool to know what kind of SQL The LINQ turns into???

LINQ = "sql


"1" LINQPad

First LINQ to SQL,,, so it is convenient for us to do optimization ...

When paging, our SQL becomes quite complex ...


Official: https://www.linqpad.net/Download.aspx


Teachers.groupjoin (Courses, (Teacher t) = T.teacherid,
(Course c) = C.tearcherid,
(t, list) = new
{
T.teachername,
List
}). Skip (1). Take (2)


--Region Parameters
DECLARE @p0 Int = 1
DECLARE @p1 Int = 2
--Endregion
SELECT [T1]. [TeacherName], [T2]. [CourseID], [T2]. [Coursename], [T2]. [Location], [T2]. [Tearcherid], (
SELECT COUNT (*)
From (
SELECT NULL as [EMPTY]
From [Course] as [T3]
WHERE ([t1].[ Teacherid]) = [T3]. [Tearcherid]
) as [T4]
) as [value]
From (
SELECT row_number () over (ORDER by [t0].[ Teacherid]) as [Row_number], [t0]. [TeacherName], [t0]. [Teacherid]
From [Teacher] as [t0]
) as [T1]
Left OUTER joins [Course] as [T2] on ([t1].[ Teacherid]) = [T2]. [Tearcherid]
WHERE [T1]. [Row_number] Between @p0 + 1 and @p0 + @p1
ORDER by [T1]. [Row_number], [T2]. [CourseID]


The Row_number () function was used ....

In this way, we can do performance optimization .... "Evaluate execution Plan"


At this time, if SQL is very slow, we can "index" the way to speed up the query performance ...


2. Sometimes the business is very complex, LINQ needs nested nesting, Crm,erp,oa ... "Report Statistics"

In LINQ complex cases, we don't know how to construct .... This is embarrassing ...

This time we know how SQL is written ...

sql = "LINQ

Linqer tools ....


Official: Http://www.sqltolinq.com/downloads

Sql:

SELECT * from Teacher as T
Join Course as C
On T.teacherid=c.tearcherid
where T.teacherid>2 and UPPER (t.teachername) = ' teacher ge ' and c.coursename like '% calendar% '
ORDER BY C.location


Linq:

From C in DB. Course
where
C.teacher.teacherid > 2 &&
C.teacher.teachername.toupper () = = "GE teacher" &&
C.coursename.contains ("Calendar")
By
C.location
Select New {
CourseID = C.courseid,
Coursename = C.coursename,
Location = C.location,
Tearcherid = C.tearcherid,
Teacherid = C.teacher.teacherid,
TeacherName = C.teacher.teachername,
Teachertype = C.teacher.teachertype
}

Learn to construct complex lambda puzzles use linqpad and linqer to achieve mutual transfer between SQL and lambda

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.