SQL statement corresponding LINQ and Lambda statements

Original Address: https://www.cnblogs.com/haoxiaozhang/p/3924736.html

1, the query all the records in the Student table Sname, Ssex and Class column.
sname SELECT, SSEx, from class Student
Linq:
    from S in Students.
    SELECT new new {
        s.SNAME,
        s.SSEX,
        s.CLASS
    }
the Lambda:
    Students.Select (S => {new new
        SNAME = s.SNAME, SSEX = S. SSEX, the CLASS = s.CLASS
    })

2, all teachers query unit does not repeat Depart column.
Depart from Teacher DISTINCT SELECT
Linq:
    from Teachers.Distinct T in ()
    SELECT t.DEPART
the Lambda:
    . Teachers.Distinct () the Select (T => t.DEPART)
. 3, a query for all records Student table.
* Student from the SELECT
Linq:
    from S Students in
    the SELECT S
the Lambda:
    Students.Select (S => S)

. 4, the query results in the table all records Score between 60 and 80.
* WHERE Score Degree from SELECT BETWEEN 60 and 80
Linq:
    from S in Scores
    WHERE s.DEGREE> = 60 && s.DEGREE <80
    SELECT S
the Lambda:
    Scores.Where ( 
        S => (
                s.DEGREE> = 60 && S. of DEGREE <80 
             )
    )
. 5, the query score score table 85, 86 or 88 of the recording.
* WHERE Score Degree from SELECT in (85,86,88)
Linq:
the In
    from S in Scores
    WHERE (
            new new decimal [] {} 85,86,88
          ) .Contains (s.DEGREE)
    SELECT S
the Lambda:
    Scores.Where (S => a Decimal new new [] {85,86,88} .Contains (s.DEGREE))
Not in
    from S in Scores
    WHERE! (
            New new decimal [] {} 85,86,88
          ) .Contains ( s.DEGREE)
    SELECT S
the Lambda:
    Scores.Where (! = S> (a Decimal new new [] {85,86,88} .Contains (s.DEGREE)))
    the Any () application: dual table Any, must be primary key (String)
    CustomerDemographics CustomerTypeID (String)
    CustomerCustomerDemos (CustomerID CustomerTypeID) (String)
    Any (or one key be Any) a primary key with two main building were
    not available, with two main keys to perform Any of a primary key
    
    from in CustomerDemographics E
    WHERE! e.CustomerCustomerDemos.Any ()
    SELECT E
    
    from C in the Categories
    WHERE! c.Products.Any ()
    c the SELECT
6, students record Student query table "95031" class or gender as "female".
select * from student where class = ' 95031' or ssex = N ' Female'
Linq:
    from S in Students.
    WHERE s.CLASS == "95031" 
       || s.CLASS == "female"
    SELECT S
the Lambda:
    Students.Where ( s => (s.CLASS == "95031 " || s.CLASS == " female"))

7, to query all of the records in descending order Class Student table.
* Order from Student by SELECT DESC Class
Linq:
    from S in Students.
    OrderBy s.CLASS Descending
    SELECT S
the Lambda:
    Students.OrderByDescending (S => s.CLASS)
. 8, Cno ascending order, Degree descending Score table records all queries.
Score * from the Order by the SELECT Cno ASC, DESC Degree
Linq:
    Scores from in S
    the OrderBy s.DEGREE Descending
    the OrderBy s.CNO Ascending 
    the SELECT S
the Lambda:
    Scores.OrderByDescending (S => s.DEGREE)
          .orderBy (S => s.CNO)

the number of students 9, the query "95031" class.
COUNT SELECT (*) from Student WHERE class = '95031'
Linq:
    (S in from Students.
        WHERE s.CLASS == "95031"
        SELECT S
    ) .Count ()
the Lambda:
    Students.Where (S => == s.CLASS "95 031")
                .Select (S => S)
                    .Count ()
10, the query score table top score of the student number and the course number.
select distinct s.Sno, c.Cno from student as  s, course as c, score as sc
where s.sno=(select sno from score where degree = (select max(degree) from score))
and c.cno = (select cno from score where degree = (select max(degree) from score))
Linq:
    (
        from s in Students
        from c in Courses
        from sc in Scores
        let maxDegree = (from sss in Scores
                        select sss.DEGREE
                        ).Max()
        let sno = (from ss in Scores
                where ss.DEGREE == maxDegree
                select ss.SNO).Single().ToString()
        let cno = (from ssss in Scores
                where ssss.DEGREE == maxDegree
                select ssss.CNO).Single().ToString()
        == && c.CNO s.sno SnO WHERE == CNO
        SELECT new new {
            s.sno,
            c.CNO
        }
    ) .Distinct ()
operation problem? Performing error: where s.SNO == sno (packets out of line) operator "==" not applicable "string" and "System.Linq.IQueryable <string>" operand type
Solution:
original: let = SnO (Scores from SS in
                WHERE == maxDegree ss.DEGREE
                SELECT ss.SNO) .ToString ()
Queryable () Single () returns the only element sequence; if the sequence is not exactly one element, an exception is thrown. 
Solution: the let SnO = (from SS Scores in
                the WHERE ss.DEGREE == maxDegree
                the SELECT ss.SNO) .Single () ToString ().
11, average query '3-105' number of courses.
AVG SELECT (Degree) Score from CNO = WHERE '3-105'
Linq:

        Scores in S from
        WHERE == s.CNO "3-105"
        SELECT s.DEGREE
    ) .Average ()
the Lambda:
    Scores.Where (S => == s.CNO "3-105")
            .Select (= S> s.DEGREE)
                .Average ()

12, the query score table has at least five students and elective courses with an average score of 3 at the beginning of.
AVG SELECT (Degree) Score from CNO like WHERE '. 3%' by Cno Group HAVING COUNT (*)> =. 5
Linq:
        from S in Scores
        WHERE s.CNO.StartsWith ( ". 3")
        Group by S s.CNO
        INTO CC
        cc.Count WHERE ()> =. 5
        SELECT cc.Average (C => c.DEGREE)
the Lambda:
    Scores.Where (S => s.CNO.StartsWith ( ". 3"))
            .
              .Where( cc => ( cc.Count() >= 5) )
                .Select( cc => cc.Average( c => c.DEGREE) )
Linq: SqlMethod
like也可以这样写:
    s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
13、查询最低分大于70,最高分小于90的Sno列。
select sno from score group by sno having min(degree) > 70 and max(degree) < 90
Linq:
    from s in Scores
    group s by s.SNO
    into ss
    where ss.Min(cc => cc.DEGREE) > 70 && ss.Max( cc => cc.DEGREE) < 90
    select new
    {
        sno = ss.Key
    }
Lambda:
    Scores.GroupBy (s => s.SNO)
               .Where (ss => ((ss.Min (cc => cc.DEGREE) > 70) && (ss.Max (cc => cc.DEGREE) < 90)))
                   .Select ( ss => new {
                                        sno = ss.Key
                                     })
14、查询所有学生的Sname、Cno和Degree列。
select s.sname,sc.cno,sc.degree from student as s,score as sc where s.sno = sc.sno
Linq:
    from s in Students
    join sc in Scores
    on s.SNO equals sc.SNO
    select new
    {
        s.SNAME,
        sc.CNO,
        sc.DEGREE
    }
Lambda:
    Students.Join(Scores, s => s.SNO,
                          sc => sc.SNO, 
                          (s,sc) => new{
                                              SNAME = s.SNAME,
                                            CNO = sc.CNO,
                                            DEGREE = sc.DEGREE
                                          })
15、查询所有学生的Sno、Cname和Degree列。
select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
Linq:
    from c in Courses
    join sc in Scores
    on c.CNO equals sc.CNO
    select new
    {
        sc.SNO,c.CNAME,sc.DEGREE
    }
Lambda:
    Courses.Join ( Scores, c => c.CNO, 
                             sc => sc.CNO, 
                             (c, sc) => new 
                                        {
                                            SNO = sc.SNO, 
                                            CNAME = c.CNAME, 
                                            DEGREE = sc.DEGREE
                                        })
16、查询所有学生的Sname、Cname和Degree列。
select s.sname,c.cname,sc.degree from student as s,course as c,score as sc where s.sno = sc.sno and c.cno = sc.cno
Linq:
    from s in Students
    from c in Courses
    from sc in Scores
    where s.SNO == sc.SNO && c.CNO == sc.CNO
    select new { s.SNAME,c.CNAME,sc.DEGREE }

Guess you like

Origin www.cnblogs.com/xbzhu/p/11462158.html