zoukankan      html  css  js  c++  java
  • 主攻ASP.NET.3.5.MVC架构之重生: LINQ(五)

    LINQ to SQL

    下载LINQ to SQL Debug Visualizer

    http://www.scottgu.com/blogposts/linqquery/sqlserverQueryVisualizer.zip

    LINQ to SQL执行标准数据库命令

    *查询文章类别ID34的文章标题并排序取4 Take()取元素条数 =>Top

    LINQ to SQL Code

                MydataDataContext data = new MydataDataContext();

                var query = data.Article.Where(m => m.CategoryID == 34)

                                        .OrderBy(m => m.Sort)

                                        .Select(m => new { m.ArticleID, m.ArticleTitle ,m.ArticleBody})

                                        .Take(4);

    SqlCode

    SELECT TOP (4) [t0].[ArticleID], [t0].[ArticleTitle], [t0].[ArticleBody]

    FROM [dbo].[Article] AS [t0]

    WHERE [t0].[CategoryID] = 34

    ORDER BY [t0].[Sort]

    *查询文章类别ID34的文章标题并排序

    LINQ to SQL Code

                MydataDataContext data = new MydataDataContext();

                var query = data.Article.Where(m => m.CategoryID == 34)

                                        .OrderBy(m => m.Sort)

                                        .Select(m => m.ArticleTitle);

    SqlCode

    SELECT [t0].[ArticleTitle]

    FROM [dbo].[Article] AS [t0]

    WHERE [t0].[CategoryID] = 34

    ORDER BY [t0].[Sort]

    *查询文章类别ID34的文章标题,文章ID,文章内容并排序

    LINQ to SQL Code

                MydataDataContext data = new MydataDataContext();

                var query = data.Article.Where(m => m.CategoryID == 34)

                                        .OrderBy(m => m.Sort)

                                        .Select(m => new { m.ArticleID, m.ArticleTitle ,m.ArticleBody});  

    SqlCode

    SELECT [t0].[ArticleID], [t0].[ArticleTitle], [t0].[ArticleBody]

    FROM [dbo].[Article] AS [t0]

    WHERE [t0].[CategoryID] = 34

    ORDER BY [t0].[Sort]

    *查询文章所有项

    LINQ to SQL Code

                MydataDataContext data = new MydataDataContext();

                var query = from m in data.Article select m;

    SqlCode

    SELECT [t0].[ArticleID], [t0].[CategoryID], [t0].[ArticleTitle], [t0].[ArticleBody], [t0].[IsTop], [t0].[Sort], [t0].[CreateTime], [t0].[SysUserID], [t0].[State]

    FROM [dbo].[Article] AS [t0]

    *查询文章选择项

    LINQ to SQL Code

                MydataDataContext data = new MydataDataContext();

                var query = data.Article.Select(m => new { m.ArticleID, m.ArticleTitle });

    SqlCode

    SELECT [t0].[ArticleID], [t0].[ArticleTitle]

    FROM [dbo].[Article] AS [t0]

    IEnumerableIEnumerable<T>接口

    IQueryProviderIQueryable<T>接口

    IQueryable

    Lambda表达式

    Code:

    MyDataDataContext data = new MyDataDataContext();                                         //使用LINQ

    var s = from n in data.Article where n.ArticleID == 21 select n;                //执行查询

    foreach (var t in s)                                                                                                            //遍历对象

    {

             Response.Write(t.ArticleTitle.ToString() + "<br/>");                             //输出对象

    }

     

     

  • 相关阅读:
    Hibernate+JPA (EntityMange讲解)
    JPA和Hibernate的区别
    Hibernate与Jpa的关系,终于弄懂
    JEE学习线路
    J2EE中你必须了解的13种技术规范
    js设置datagriad的行移动
    js正则表达式中的特殊字符
    iOS 开发之动画篇
    Phone APP设计规范/iPad APP设计规范/Android APP设计规范/网页设计规范
    打包程序时的证书问题(上传APP就出现Missing iOS Distribution signing indetity for)
  • 原文地址:https://www.cnblogs.com/cube/p/2524121.html
Copyright © 2011-2022 走看看