小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

通過(guò)16道練習(xí)學(xué)習(xí)Linq和Lambda

 滅星之破 2012-02-10

1、 查詢(xún)Student表中的所有記錄的Sname、SsexClass列。
select sname,ssex,class from student
Linq:
    from s in Students
    select new {
        s.SNAME,
        s.SSEX,
        s.CLASS
    }
Lambda:
    Students.Select( s => new {
        SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
    })


2
、 查詢(xún)教師所有的單位即不重復(fù)的Depart列。
select distinct depart from teacher
Linq:
    from t in Teachers.Distinct()
    select t.DEPART
Lambda:
    Teachers.Distinct().Select( t => t.DEPART)

 

3、 查詢(xún)Student表的所有記錄。
select * from student
Linq:
    from s in Students
    select s
Lambda:
    Students.Select( s => s)


4
、 查詢(xún)Score表中成績(jī)?cè)?/SPAN>6080之間的所有記錄。
select * from score where degree between 60 and 80
Linq:
    from s in Scores
    where s.DEGREE >= 60 && s.DEGREE < 80
    select s

Lambda:
    Scores.Where(
        s => (
                s.DEGREE >= 60 && s.DEGREE < 80
             )
    )

 

5、 查詢(xún)Score表中成績(jī)?yōu)?/SPAN>85,8688的記錄。
select * from score where degree in (85,86,88)
Linq:
In
    from s in Scores
    where (
            new decimal[]{85,86,88}
          ).Contains(s.DEGREE)
    select s
Lambda:
    Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
Not in
    from s in Scores
    where !(
            new decimal[]{85,86,88}
          ).Contains(s.DEGREE)
    select s
Lambda:
    Scores.Where( s => !(new Decimal[]{85,86,88}.Contains(s.DEGREE)))

    Any()應(yīng)用:雙表進(jìn)行Any時(shí),必須是主鍵為(String)
    CustomerDemographics CustomerTypeID
String
    CustomerCustomerDemos (CustomerID CustomerTypeID) (String)
   
一個(gè)主鍵與二個(gè)主建進(jìn)行Any(或者是一對(duì)一關(guān)鍵進(jìn)行Any)
   
不可,以二個(gè)主鍵于與一個(gè)主鍵進(jìn)行Any
   
    from e in CustomerDemographics
    where !e.CustomerCustomerDemos.Any()
    select e
   
    from c in Categories
    where !c.Products.Any()
    select c

 

6、 查詢(xún)Student表中"95031"班或性別為""的同學(xué)記錄。
select * from student where class ='95031' or ssex= N'
'
Linq:
    from s in Students
    where s.CLASS == "95031"
       || s.CLASS == "
"
    select s
Lambda:
    Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "
"))


7
、 Class降序查詢(xún)Student表的所有記錄。
select * from student order by Class DESC
Linq:
    from s in Students
    orderby s.CLASS descending
    select s
Lambda:
    Students.OrderByDescending(s => s.CLASS)

 

8 Cno升序、Degree降序查詢(xún)Score表的所有記錄。
select * from score order by Cno ASC,Degree DESC
Linq:(
這里Cno ASClinq中要寫(xiě)在最外面)
    from s in Scores
    orderby s.DEGREE descending
    orderby s.CNO ascending
    select s
Lambda:
    Scores.OrderByDescending( s => s.DEGREE)
          .OrderBy( s => s.CNO)


9
查詢(xún)"95031"班的學(xué)生人數(shù)。
select count(*) from student where class = '95031'
Linq:
    (    from s in Students
        where s.CLASS == "95031"
        select s
    ).Count()
Lambda:
    Students.Where( s => s.CLASS == "95031" )
                .Select( s => s)
                    .Count()

 

10、查詢(xún)Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)。
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()
        where s.SNO == sno && c.CNO == cno
        select new {
            s.SNO,
            c.CNO
        }
    ).Distinct()
操作時(shí)問(wèn)題?執(zhí)行時(shí)報(bào)錯(cuò): where s.SNO == sno(這行報(bào)出來(lái)的) 運(yùn)算符"=="無(wú)法應(yīng)用于"string""System.Linq.IQueryable<string>"類(lèi)型的操作數(shù)
解決:
原:let sno = (from ss in Scores
                where ss.DEGREE == maxDegree
                select ss.SNO).ToString()
Queryable().Single()
返回序列的唯一元素;如果該序列并非恰好包含一個(gè)元素,則會(huì)引發(fā)異常。
解:let sno = (from ss in Scores
                where ss.DEGREE == maxDegree
                select ss.SNO).Single().ToString()
 

11、查詢(xún)'3-105'號(hào)課程的平均分。
select avg(degree) from score where cno = '3-105'
Linq:
    (
        from s in Scores
        where s.CNO == "3-105"
        select s.DEGREE
    ).Average()
Lambda:
    Scores.Where( s => s.CNO == "3-105")
            .Select( s => s.DEGREE)
                .Average()


12
、查詢(xún)Score表中至少有5名學(xué)生選修的并以3開(kāi)頭的課程的平均分?jǐn)?shù)。
select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5
Linq:
        from s in Scores
        where s.CNO.StartsWith("3")
        group s by s.CNO
        into cc
        where cc.Count() >= 5
        select cc.Average( c => c.DEGREE)
Lambda:
    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
like
也可以這樣寫(xiě):
    s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")

 

13、查詢(xún)最低分大于70,最高分小于90Sno列。
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、查詢(xún)所有學(xué)生的SnameCnoDegree列。
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、查詢(xún)所有學(xué)生的Sno、CnameDegree列。
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、查詢(xún)所有學(xué)生的Sname、CnameDegree列。
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 }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多