Differences between SQL, LINQ, and lambda query statements

Source: Internet
Author: User

The writing format for LINQ is as follows:
From Temp variable in collection object or database object
Where Condition expression
[ORDER BY condition]
The value queried in the Select Temp variable
[GROUP BY conditions]

The lambda expression is written in the following format:

(parameter list) = = expression or block of statements

Where: Number of parameters: can have more than one parameter, one parameter, or no parameter.

Parameter type: can be defined implicitly or explicitly.

expression or block of statements: This part is the implementation part of the function we normally write (function body).

1. Search All

1 query all records of the student table.
2 Select * FROM student
3 Linq:
4 from s in Students
5 Select S
6 Lambda:
7

2 Search All by condition:

1  Query the sname, Ssex, and class columns of all records in the student table.
2 Select Sname,ssex,class from student
3 Linq:
4 from s in Students
5 Select New {
6 S.sname,
7 S.ssex,
8 S.class
9 }
Ten LAMBDA:
Students.select (s = = new {
SNAME = S.sname,ssex = S.ssex,class = S.class
})

3.distinct Remove the duplicate

1 Query teachers All units are not duplicates of the depart column.
2 SELECT distinct depart from teacher
3 Linq:
4 from T in Teachers.distinct ()
5 Select T.depart
6 Lambda:
7

4. Connection query between and

1 query all records in the score table with scores from 60 to 80.
2 Select * from score where degree between and 80
3 Linq:
4 from s in Scores
5 where S.degree >= && S.degree < 80
6 Select S
7 LAMBDA:
8 Scores.where (
9 s = (
s.degree >= && S.degree < 80
One )
12

5. Filter in within range

1 SELECT * from score where degree in (85,86,88)
2 Linq:
3 from s in Scores
4 WHERE (
5 New Decimal[]{85,86,88}
6 ). Contains (S.degree)
7 Select S
8 Lambda:
9 scores.where (s = = new decimal[] {85,86,88}. Contains (S.degree))

6.or Conditional filtering

1 Check the student table in the "95031" class or sex for "female" students record.
2 Select * FROM student where class = ' 95031 ' or ssex= N ' female '
3 Linq:
4 from s in Students
5 where S.class = = "95031"
6 | | s.class = = "female"
7 Select S
8 Lambda:
9

7. Sorting

1 queries all records of the student table in descending order of class.
2 SELECT * FROM Student ORDER by Class DESC
3 Linq:
4 from s in Students
5 S.class Descending
6 Select S
7 LAMBDA:
8 students.orderbydescending (s = s.class)

8.count () Row count query

1 Select COUNT (*) from student where class = ' 95031 '
2 Linq:
3 (from s in Students
4 where S.class = = "95031"
5 Select S
6 ). Count ()
7 LAMBDA:
8 students.where (s = = S.class = = "95031")
9 . Select (s = s)
. Count ()

10.avg () Average

1 Check the average of the ' 3-105 ' course.
2 Select AVG (degree) from score where cno = ' 3-105 '
3 Linq:
4 (
5 from s in Scores
6 where s.cno = = "3-105"
7 Select S.degree
8 ). Average ()
9 Lambda:
Ten scores.where (s = = S.cno = = "3-105")
One by one . Select (s = = S.degree)

11. Sub-Query

1 Check the student number and course number of the highest score in the score table.
2 SELECT distinct S.SNO,C.CNO from student as s,course as C, score as SC
3 Where s.sno= (select Sno from score where degree = (select Max (degree) from score))
4 and C.cno = (select CNO from score where degree = (select Max (degree) from score))
5 Linq:
6 (
7 from S in Students
8 from C in Courses
9 from SC in Scores
Maxdegree = (from SSS in Scores
Select SSS. Degree
12). Max ()
Sno = (from the SS in Scores
The Where SS. degree = = Maxdegree
Select SS. SNO). Single (). ToString ()
CNO = (from ssss in Scores
The Where ssss. degree = = Maxdegree
Select SSSs. CNO). Single (). ToString ()
where S.sno = = SNO && C.cno = = CNO
Select New {
S.sno,
C.cno
23}
24). Distinct ()

12. Packet filtering

1 Check the average score of the course with at least 5 students enrolled in the score table and begin with 3.
2 Select AVG (degree) from score where CNO like ' 3% ' GROUP by CNO have Count (*) >=5
3 Linq:
4 from s in Scores
5 where S.cno. StartsWith ("3")
6 Group S by s.cno
7 into cc
8 where CC. Count () >= 5
9 Select CC. Average (c = c.degree)
Ten LAMBDA:
One scores.where (s = = S.cno. StartsWith ("3"))
. GroupBy (s = s.cno)
. Where (cc = (cc. Count () >= 5))
. Select (cc = CC. Average (c = c.degree))
Linq:sqlmethod
It can also be written like this:
17

13. Grouping

1 Check the average score of the course with at least 5 students enrolled in the score table and begin with 3.
2 Select AVG (degree) from score where CNO like ' 3% ' GROUP by CNO have Count (*) >=5
3 Linq:
4 from s in Scores
5 where S.cno. StartsWith ("3")
6 Group S by s.cno
7 into cc
8 where CC. Count () >= 5
9 Select CC. Average (c = c.degree)
Ten LAMBDA:
One scores.where (s = = S.cno. StartsWith ("3"))
. GroupBy (s = s.cno)
. Where (cc = (cc. Count () >= 5))
. Select (cc = CC. Average (c = c.degree))
Linq:sqlmethod
It can also be written like this:
17

14. Multi-Table Query

1 Select Sc.sno,c.cname,sc.degree from course as c,score as SC where c.cno = Sc.cno
2 Linq:
3 from C in Courses
4 Join SC in Scores
5 on c.cno equals SC. CNO
6 Select New
7 {
8 SC. Sno,c.cname,sc. Degree
9 }
Ten LAMBDA:
Courses.join (Scores, C = c.cno,
SC = sc. CNO,
(c, SC) = new
{
SNO = sc. SNO,
the CNAME = C.cname,
degree = sc. Degree
})
19

Differences between SQL, LINQ, and lambda query statements

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.