广告
返回顶部
首页 > 资讯 > 数据库 > SQL Linq Lambda
  • 612
分享到

SQL Linq Lambda

摘要

  1、 查询Student表中的所有记录的Sname、Ssex和Class列。select sname,ssex,class from studentLinq:    from s in Students    selec


	SQL   Linq   Lambda
[数据库教程]

 

1、 查询Student表中的所有记录的Sname、Ssex和Class列。
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、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
Linq:
    from t in Teachers.Distinct()
    select t.DEPART
Lambda:
    Teachers.Distinct().Select( t => t.DEPART)


3、 查询Student表的所有记录。
select * from student
Linq:
    from s in Students
    select s
Lambda:
    Students.Select( s => s)

4、 查询Score表中成绩在60到80之间的所有记录。
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、 查询Score表中成绩为85,86或88的记录。
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()应用:双表进行Any时,必须是主键为(String)
    CustomerDemographics CustomerTypeID(String)
    CustomerCustomerDemos (CustomerID CustomerTypeID) (String)
    一个主键与二个主建进行Any(或者是一对一关键进行Any)
    不可,以二个主键于与一个主键进行Any
    
    from e in CustomerDemographics
    where !e.CustomerCustomerDemos.Any()
    select e
    
    from c in CateGories
    where !c.Products.Any()
    select c
6、 查询Student表中"95031"班或性别为"女"的同学记录。
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降序查询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降序查询Score表的所有记录。
select * from score order by Cno ASC,Degree DESC
Linq:(这里Cno ASC在linq中要写在最外面)
    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、 查询"95031"班的学生人数。
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、查询Score表中的最高分的学生学号和课程号。
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()
操作时问题?执行时报错: where s.SNO == sno(这行报出来的) 运算符"=="无法应用于"string"和"System.Linq.IQueryable"类型的操作数
解决:
原:let sno = (from ss in Scores
                where ss.DEGREE == maxDegree
                select ss.SNO).ToString()
Queryable().Single()返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。 
解:let sno = (from ss in Scores
                where ss.DEGREE == maxDegree
                select ss.SNO).Single().ToString()
11、查询‘3-105‘号课程的平均分。
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、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
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也可以这样写:
    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 }

 

////////////

 

SQL Linq Lambda

原文地址:https://www.cnblogs.com/wfy680/p/14254079.html

您可能感兴趣的文档:

--结束END--

本文标题: SQL Linq Lambda

本文链接: https://www.lsjlt.com/news/8015.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SQL Linq Lambda
      1、 查询Student表中的所有记录的Sname、Ssex和Class列。select sname,ssex,class from studentLinq:    from s in Students    selec...
    99+
    2016-08-13
    SQL Linq Lambda 数据库入门 数据库基础教程 数据库 mysql
  • Linq Lambda表达式是什么
    本篇内容介绍了“Linq Lambda表达式是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Linq Lambda表达式许多标准查询操作...
    99+
    2023-06-17
  • LINQ中Lambda表达式怎么用
    小编给大家分享一下LINQ中Lambda表达式怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Linq有很多值得学习的地方,这里我们主要介绍LINQ Lamb...
    99+
    2023-06-17
  • Linq Lambda表达式怎么使用
    本篇内容介绍了“Linq Lambda表达式怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C#3.0时代的Linq查询语句在C#3....
    99+
    2023-06-17
  • LINQ教程之使用Lambda表达式
    C#3.0(.NET3.5)中引入了Lambda表达式和LINQ。Lambda表达式是使用一些特殊语法表示匿名方法的较短方法。 一、最基本的Lambda表达式语法如下: (参数列表)...
    99+
    2022-11-13
  • Linq lambda表达式的示例分析
    小编给大家分享一下Linq lambda表达式的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Linq lambda表达式了解过C# 3.0的新特性的话应...
    99+
    2023-06-17
  • Linq to sql是什么
    这篇文章主要讲解了“Linq to sql是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linq to sql是什么”吧!什么是Linq to sqlLinq to sql(或者叫DL...
    99+
    2023-06-17
  • Linq To SQL如何输出SQL语句
    这篇文章将为大家详细讲解有关Linq To SQL如何输出SQL语句,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。最近在使用Linq To SQL的时候,为了了解不同Linq语句对性能造成的不同影响,需要...
    99+
    2023-06-17
  • LINQ to SQL有什么用
    这篇文章将为大家详细讲解有关LINQ to SQL有什么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。LINQ to SQL 目前只支持SQL Server(SQL Server Compact版本正在...
    99+
    2023-06-17
  • LINQ to SQL能做什么
    本篇内容主要讲解“LINQ to SQL能做什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LINQ to SQL能做什么”吧!一. LINQ to Objects只要实现了IEnumerab...
    99+
    2023-06-17
  • Linq to SQL的DataContext怎么写
    本篇内容介绍了“Linq to SQL的DataContext怎么写”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!强类型DataContex...
    99+
    2023-06-17
  • Linq To Sql缺点是什么
    本篇内容主要讲解“Linq To Sql缺点是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linq To Sql缺点是什么”吧!Linq To Sql缺点很久前,有个网友问到这么一个问题。...
    99+
    2023-06-17
  • Linq To Sql优点是什么
    这篇文章主要讲解了“Linq To Sql优点是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linq To Sql优点是什么”吧!Linq To Sql优点在Linq To Sql推出...
    99+
    2023-06-17
  • Linq to sql是什么意思
    这篇文章给大家分享的是有关Linq to sql是什么意思的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。什么是Linq to sqlLinq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一...
    99+
    2023-06-17
  • LINQ to SQL查询和SQL命令怎么使用
    这篇文章主要讲解了“LINQ to SQL查询和SQL命令怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“LINQ to SQL查询和SQL命令怎么使用”吧!直接执行LINQ to S...
    99+
    2023-06-17
  • LINQ to SQL如何输出参数
    这篇文章将为大家详细讲解有关LINQ to SQL如何输出参数,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。LINQ to SQL输出参数映射到引用参数,并且对于值类型,它将参数声明为可以为 null。下...
    99+
    2023-06-17
  • LINQ to SQL数据表怎么用
    这篇文章主要为大家展示了“LINQ to SQL数据表怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“LINQ to SQL数据表怎么用”这篇文章吧。在LINQ to SQL数据表里面都是用...
    99+
    2023-06-17
  • Linq To Sql性能举例分析
    这篇文章主要讲解了“Linq To Sql性能举例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linq To Sql性能举例分析”吧!Linq To Sql性能Linq To Sql性...
    99+
    2023-06-17
  • LINQ to SQL映射怎么实现
    本篇内容介绍了“LINQ to SQL映射怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!LINQ to SQL映射函数和存储过程LI...
    99+
    2023-06-17
  • LINQ To SQL和ORM怎么理解
    这篇文章主要讲解了“LINQ To SQL和ORM怎么理解”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“LINQ To SQL和ORM怎么理解”吧!LINQ To SQL和ORM的理解1、没...
    99+
    2023-06-17
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作