精华内容
下载资源
问答
  • Linq语法

    2019-09-26 07:03:41
    希望能帮助一些linq新手。 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的...1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; ...

    希望能帮助一些linq新手。

    开门见山

    读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦。

    1.简单的linq语法

    复制代码
                //1
                var ss = from r in db.Am_recProScheme
                         select r;
                //2
                var ss1 = db.Am_recProScheme;
                //3
                string sssql = "select * from Am_recProScheme";
    复制代码

    2.带where的查询

    复制代码
                //1
                var ss = from r in db.Am_recProScheme
                         where r.rpId > 10
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
                //3
                string sssql = "select * from Am_recProScheme where rpid>10";
    复制代码

    3.简单的函数计算(count,min,max,sum)

    复制代码
                //1
                ////获取最大的rpId
                //var ss = (from r in db.Am_recProScheme
                //          select r).Max(p => p.rpId);
                ////获取最小的rpId
                //var ss = (from r in db.Am_recProScheme
                //          select r).Min(p => p.rpId);
                //获取结果集的总数
                //var ss = (from r in db.Am_recProScheme                  
                //         select r).Count();
                //获取rpId的和
                var ss = (from r in db.Am_recProScheme
                          select r).Sum(p => p.rpId);
    
    
                //2
                //var ss1 = db.Am_recProScheme.Max(p=>p.rpId);
                //var ss1 = db.Am_recProScheme.Min(p => p.rpId);
                //var ss1 = db.Am_recProScheme.Count() ;
                var ss1 = db.Am_recProScheme.Sum(p => p.rpId);
                Response.Write(ss);
    
                //3
                string sssql = "select max(rpId) from Am_recProScheme";
                       sssql = "select min(rpId) from Am_recProScheme";
                       sssql = "select count(1) from Am_recProScheme";
                       sssql = "select sum(rpId) from Am_recProScheme";
    复制代码

    4.排序order by desc/asc

    复制代码
                var ss = from r in db.Am_recProScheme
                         where r.rpId > 10
                         orderby r.rpId descending  //倒序
                         //  orderby r.rpId ascending   //正序
                         select r;
    
                //正序
                var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
                //倒序
                var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList();
    
                string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";
    复制代码

    5.top(1)

    复制代码
                //如果取最后一个可以按倒叙排列再取值
                var ss = (from r in db.Am_recProScheme                     
                          select r).FirstOrDefault();
    
                //()linq to ef 好像不支持 Last() 
                var ss1 = db.Am_recProScheme.FirstOrDefault();
                //var ss1 = db.Am_recProScheme.First();          
    
                string sssql = "select top(1) * from Am_recProScheme";
    复制代码

    6.跳过前面多少条数据取余下的数据

    复制代码
                //1
                var ss = (from r in db.Am_recProScheme
                          orderby r.rpId descending
                          select r).Skip(10); //跳过前10条数据,取10条之后的所有数据   
                //2  
                var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();
                //3
                string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
    复制代码

    7.分页数据查询

    复制代码
                //1
                var ss = (from r in db.Am_recProScheme
                          where r.rpId > 10
                          orderby r.rpId descending
                          select r).Skip(10).Take(10); //取第11条到第20条数据                   
    
                //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
                var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
                //3
                string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";
    复制代码

    8.包含,类似like '%%'

    复制代码
                //1
                var ss = from r in db.Am_recProScheme
                         where r.SortsText.Contains("张")
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
                //3
                string sssql = "select * from Am_recProScheme where SortsText like '%张%'";
    复制代码

    9.分组group by

    复制代码
                //1
                var ss = from r in db.Am_recProScheme
                         orderby r.rpId descending
                         group r by r.recType into n
                         select new
                         {
                             n.Key,  //这个Key是recType
                             rpId = n.Sum(r => r.rpId), //组内rpId之和
                             MaxRpId = n.Max(r => r.rpId),//组内最大rpId
                             MinRpId = n.Min(r => r.rpId), //组内最小rpId
                         };
                foreach (var t in ss)
                {
                    Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
                }
                //2
                var ss1 = from r in db.Am_recProScheme
                         orderby r.rpId descending
                         group r by r.recType into n
                         select n;
                foreach (var t in ss1)
                {
                    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
                }
                //3
                var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
                foreach (var t in ss2)
                {
                    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
                }
                //4
                string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
    复制代码

    10.连接查询 

    复制代码
                //1
                var ss = from r in db.Am_recProScheme
                         join w in db.Am_Test_Result on r.rpId equals w.rsId
                         orderby r.rpId descending
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
                //3
                string sssql = "select r.* from  [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] = t.[rsId] order by r.[rpId] desc";
    复制代码

    11.sql中的In

    复制代码
                //1
                var ss = from p in db.Am_recProScheme
                                  where (new int?[] { 24, 25,26 }).Contains(p.rpId)
                                  select p;
                foreach (var p in ss)
                {
                    Response.Write(p.Sorts);
                }
                //2
                string st = "select * from Am_recProScheme where rpId in(24,25,26)";
    复制代码

     

    如果你是linq新手,建议这十个常用的查询标记学习,再加上视图,基本上就能解决大部分的查询问题啦。

    转载:http://www.cnblogs.com/knowledgesea/p/3897665.html

     

    转载于:https://www.cnblogs.com/Gxiaopan/p/4294194.html

    展开全文
  • Linq 语法

    2018-07-31 14:18:00
    1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; //3 ...

    1.简单的linq语法

                //1
                var ss = from r in db.Am_recProScheme
                         select r;
                //2
                var ss1 = db.Am_recProScheme;
                //3
                string sssql = "select * from Am_recProScheme";

    2.带where的查询

                //1
                var ss = from r in db.Am_recProScheme
                         where r.rpId > 10
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
                //3
                string sssql = "select * from Am_recProScheme where rpid>10";

    3.简单的函数计算(count,min,max,sum)

                //1
                ////获取最大的rpId
                //var ss = (from r in db.Am_recProScheme
                //          select r).Max(p => p.rpId);
                ////获取最小的rpId
                //var ss = (from r in db.Am_recProScheme
                //          select r).Min(p => p.rpId);
                //获取结果集的总数
                //var ss = (from r in db.Am_recProScheme                  
                //         select r).Count();
                //获取rpId的和
                var ss = (from r in db.Am_recProScheme
                          select r).Sum(p => p.rpId);
    
    
                //2
                //var ss1 = db.Am_recProScheme.Max(p=>p.rpId);
                //var ss1 = db.Am_recProScheme.Min(p => p.rpId);
                //var ss1 = db.Am_recProScheme.Count() ;
                var ss1 = db.Am_recProScheme.Sum(p => p.rpId);
                Response.Write(ss);
    
                //3
                string sssql = "select max(rpId) from Am_recProScheme";
                       sssql = "select min(rpId) from Am_recProScheme";
                       sssql = "select count(1) from Am_recProScheme";
                       sssql = "select sum(rpId) from Am_recProScheme";

    4.排序order by desc/asc

                var ss = from r in db.Am_recProScheme
                         where r.rpId > 10
                         orderby r.rpId descending  //倒序
                         //  orderby r.rpId ascending   //正序
                         select r;
    
                //正序
                var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
                //倒序
                var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList();
    
                string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";

    5.分页数据查询

                //1
                var ss = (from r in db.Am_recProScheme
                          where r.rpId > 10
                          orderby r.rpId descending
                          select r).Skip(10).Take(10); //取第11条到第20条数据                   
    
                //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
                var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
                //3
                string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";

    6.包含,类似like '%%'

                //1
                var ss = from r in db.Am_recProScheme
                         where r.SortsText.Contains("")
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
                //3
                string sssql = "select * from Am_recProScheme where SortsText like '%张%'";

    7.分组group by

                //1
                var ss = from r in db.Am_recProScheme
                         orderby r.rpId descending
                         group r by r.recType into n
                         select new
                         {
                             n.Key,  //这个Key是recType
                             rpId = n.Sum(r => r.rpId), //组内rpId之和
                             MaxRpId = n.Max(r => r.rpId),//组内最大rpId
                             MinRpId = n.Min(r => r.rpId), //组内最小rpId
                         };
                foreach (var t in ss)
                {
                    Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
                }
                //2
                var ss1 = from r in db.Am_recProScheme
                         orderby r.rpId descending
                         group r by r.recType into n
                         select n;
                foreach (var t in ss1)
                {
                    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
                }
                //3
                var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
                foreach (var t in ss2)
                {
                    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
                }
                //4
                string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";

    8.连接查询 

                //1
                var ss = from r in db.Am_recProScheme
                         join w in db.Am_Test_Result on r.rpId equals w.rsId
                         orderby r.rpId descending
                         select r;
                //2
                var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
                //3
                string sssql = "select r.* from  [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] = t.[rsId] order by r.[rpId] desc";

    9.sql中的In

                //1
                var ss = from p in db.Am_recProScheme
                                  where (new int?[] { 24, 25,26 }).Contains(p.rpId)
                                  select p;
                foreach (var p in ss)
                {
                    Response.Write(p.Sorts);
                }
                //2
                string st = "select * from Am_recProScheme where rpId in(24,25,26)";

     

    转载于:https://www.cnblogs.com/zj19940610/p/9395596.html

    展开全文
  • linQ语法

    2018-06-20 18:46:18
    转自:http://www.cnblogs.com/knowledgesea/p/3897665.html每一种搜索结果集,都以三种方式变现出来1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 ...

    转自:http://www.cnblogs.com/knowledgesea/p/3897665.html

    每一种搜索结果集,都以三种方式变现出来

    1.简单的linq语法

    //1
    var ss = from r in db.Am_recProScheme
             select r;
    //2
    var ss1 = db.Am_recProScheme;
    //3
    string sssql = "select * from Am_recProScheme";

    2.带where的查询

    //1
    var ss = from r in db.Am_recProScheme
             where r.rpId > 10
              select r;
    
    //2
    var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
    
    //3
    string sssql = "select * from Am_recProScheme where rpid>10";

    3.简单的函数计算(count,min,max,sum)

    //1
    //获取最大的rpId
    var ss = (from r in db.Am_recProScheme
              select r).Max(p => p.rpId);
    //获取最小的rpId
    var ss = (from r in db.Am_recProScheme
              select r).Min(p => p.rpId);
    //获取结果集的总数
    var ss = (from r in db.Am_recProScheme                  
              select r).Count();
    //获取rpId的和
    var ss = (from r in db.Am_recProScheme
               select r).Sum(p => p.rpId);
    
    
    //2
    var ss1 = db.Am_recProScheme.Max(p=>p.rpId);
    var ss1 = db.Am_recProScheme.Min(p => p.rpId);
    var ss1 = db.Am_recProScheme.Count() ;
    var ss1 = db.Am_recProScheme.Sum(p => p.rpId);
    Response.Write(ss);
    
    //3
    string sssql = "select max(rpId) from Am_recProScheme";
           sssql = "select min(rpId) from Am_recProScheme";
           sssql = "select count(1) from Am_recProScheme";
           sssql = "select sum(rpId) from Am_recProScheme";

    4.排序order by desc/asc

    var ss = from r in db.Am_recProScheme
             where r.rpId > 10
             orderby r.rpId descending  //倒序
             //  orderby r.rpId ascending   //正序
             select r;
    
    //正序
    var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
    //倒序
    var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList();
    
    string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";

    5.top(1)

    //如果取最后一个可以按倒叙排列再取值
    var ss = (from r in db.Am_recProScheme                     
              select r).FirstOrDefault();
    
    //()linq to ef 好像不支持 Last() 
    var ss1 = db.Am_recProScheme.FirstOrDefault();
    //var ss1 = db.Am_recProScheme.First();          
    
    string sssql = "select top(1) * from Am_recProScheme";

    6.跳过前面多少条数据取余下的数据

    //1
    var ss = (from r in db.Am_recProScheme
              orderby r.rpId descending
    select r).Skip(10); //跳过前10条数据,取10条之后的所有数据   
    
    //2  
    var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();
    
    //3
    string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";

    7.分页数据查询

    //1
    var ss = (from r in db.Am_recProScheme
              where r.rpId > 10
              orderby r.rpId descending
              select r).Skip(10).Take(10); //取第11条到第20条数据                   
    
    //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
    var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
    
    //3
    string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";

    8.包含,类似like '%%'

    //1
    var ss = from r in db.Am_recProScheme
             where r.SortsText.Contains("张")
             select r;
    
    //2
    var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
    
    //3
    string sssql = "select * from Am_recProScheme where SortsText like '%张%'";

    9.分组group by

    //1
    var ss = from r in db.Am_recProScheme
             orderby r.rpId descending
             group r by r.recType into n
             select new
             {
                 n.Key,  //这个Key是recType
                 rpId = n.Sum(r => r.rpId), //组内rpId之和
                 MaxRpId = n.Max(r => r.rpId),//组内最大rpId
                 MinRpId = n.Min(r => r.rpId), //组内最小rpId
              };
    foreach (var t in ss)
    {
        Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
    }
    
    //2
    var ss1 = from r in db.Am_recProScheme
              orderby r.rpId descending
              group r by r.recType into n
              select n;
    foreach (var t in ss1)
    {
        Response.Write(t.Key + "--" + t.Min(p => p.rpId));
    }
    
    //3
    var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
    foreach (var t in ss2)
    {
        Response.Write(t.Key + "--" + t.Min(p => p.rpId));
    }
    //4
    string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";

    10.连接查询 

    //1
    var ss = from r in db.Am_recProScheme
             join w in db.Am_Test_Result on r.rpId equals w.rsId
             orderby r.rpId descending
             select r;
    
    //2
    var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
    
    //3
    string sssql = "select r.* from  [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] = t.[rsId] order by r.[rpId] desc";

    11.sql中的In

    //1
    var ss = from p in db.Am_recProScheme
             where (new int?[] { 24, 25,26 }).Contains(p.rpId)
             select p;
    foreach (var p in ss)
    {
        Response.Write(p.Sorts);
    }
    
    //2
    string st = "select * from Am_recProScheme where rpId in(24,25,26)";

    展开全文
  • LINQ语法

    2010-03-27 20:06:00
    学习LINQ时,经常会遇到LINQ语法问题,这里将介绍LINQ语法问题的解决方法,以及介绍LINQ 是一系列语言延伸模块,以型别安全的方式支持数据查询。 程序语言随着时间演进,一再沉淀经验与抽象后,以简练直观的语法...

    学习LINQ时,经常会遇到LINQ语法问题,这里将介绍LINQ语法问题的解决方法,以及介绍LINQ 是一系列语言延伸模块,以型别安全的方式支持数据查询。

    程序语言随着时间演进,一再沉淀经验与抽象后,以简练直观的语法解决具有共通特征的各式问题。Visual Studio 2008(程序代码名称为 Orcas)、C# 3.0 与 VB.NET 9.0 后将支持新的语法 Language Integrated Query(LINQ),想一体解决多样的资料存取。它是由 Anders Hejlsberg 所主导。Anders 曾打造了 Turbo Pascal、Delphi、Visual J++、C# 等叫好叫座的产品,单凭这位杀手应用创造者的眼光,LINQ 就不可小觑。

    LINQ 是一系列语言延伸模块,以型别安全的方式支持数据查询。期待隔绝各种数据的特性,不管是各厂家数据库的 SQL 方言,或是 XML 的 DOM、XQuery 或 XPath,抑或是对象集合的属性存取。以共通的方式完成数据操作,如:挑选、比对、排序、汇总…等等。期待减轻程序开发人员学习操作各种数据的负荷。

    LINQ 藉由各语言编译程序将内嵌的 LINQ语法转译成原本的 C# 或 VB.NET 程序代码,并呼叫相关的底层模块以实体维护数据。最后编译成与 .NET Framework 2.0 CLR 兼容的 IL,所以 CLR 本身并未增加与 LINQ 相关的模块,但 .NET Framework、Visual Studio 整合开发环境和程序语言需要增加相关功能和语法。

    就笔者自己的感觉,LINQ 有以下的好处:

    ◆简化大量的细节运作,将如何(how)取得数据换成要操作什么(what)数据:这隐含存取优化交由专家来做,如 DB 引擎优化存取数据。

    ◆透过IEnumerable<T> 一致性地存取各种数据,并在查询语法中互相整合:如 File System、操作系统的 process、Registry、对象集合、XML、DB...,所用的皆是对象,大家的属性都是数据。换句话说,数据对象化,对象数据化,存取二者的语法与语意相同。

    ◆平行运算:若要处理大量数据,程序设计师不容易撰写 for 循环还包含平行运算。但 LINQ 转译成 C# 或 VB.NET 的程序代码时,可以平行运算的方式处理大量数据。

    ◆撰写数据对象存取的过程中,可以 IntelliSense 和强型别检查:相较于以往 ADO.NET 加上 SQL 语法,LINQ 比 typed dataset 直观易懂,且应用更为广泛。

    就笔者与许多朋友聊到 LINQ 时,最多的询问便是「未来是否不需要学 SQL 了?」个人认为,短时间不可能,LINQ 或许会减轻程序设计师对 SQL 的倚赖,但 LINQ 不会取代 SQL。它们各有一片天,LINQ 是程序设计师讲的数据对象语言,SQL 是数据库管理师对数据库引擎讲的语言,LINQ 是从应用程序处理数据的角度出发,但 SQL 关乎着整体数据库服务器有效且安全地活着的每一个细节。

    而紧接的问题通常是:「将数据以对象来包装,透过 entity 类别间接存取数据,那是否会有效率问题?」这笔者无法回答,尚待真实世界来证明。

    LINQ 范例

    你可以在 C# 中直接内嵌如下的语法:取得在 Customer 对象集合中,每个 Customer 的属性 Country 值为 USA,按照 City 属性由大到小排序,传回以 CompanyName 和 City 两个属性的字符串值所建立的新对象之集合:

    1. var matchCustomers = from c in db.Customers  
    2. where c.Country == "USA"   
    3. orderby c.City descending  
    4. select new { c.CompanyName, c.City }; 

    这句 LINQ语法经由 C# 编译程序解析,传回实做 IEnumerable<Anonymous Type> 接口的对象给 matchCustomers 变量。而在查询语法中,呼叫了 where、orderby、select 等延伸方法(Extension Methods)。并定义匿名方法(Anonymous Methods)的委派(delegate);要求 where 方法比较 Customers 集合中 Customer 对象的 Country 属性值为 USA。换句话说,自动将 c.Country == "USA" 转成 bool Pred<T>(T item) 形式的委派。最后透过 select 延伸方法搭配对象初始化表达式,回传匿名型别(Anonymous Type)对象的集合。

    若不采用 LINQ 写法,上述语法也可以写成如下的方式:

    1. var matchCustomers = db.Customers.Where(c => c.Country == "USA").
      OrderByDescending(
      c => c.City).Select(c => new { c.CompanyName, c.City }); 

    哇,想我修习 .NET 数年,竟不知所云!?在此介绍一本入门书:「Introducing Microsoft LINQ」,Microsoft Press 出版。它让你了解以往的 C# 和 VB.NET 各版本如何渐进地增加功能,最后演变出 LINQ语法。

     

     

    而LINQ查询语法跟SQL查询语法很相识,除了先后顺序。

      Q:为何 LINQ 查询语法是以 from 关键字开头的,而不是以 select 关键字开头的?select 开头这种写法跟SQL的写法更接近,更易懂呀?

      A:简单来说,为了IDE的智能感知(Intelisence)这个功能,select 关键字放在后面了。

      编程语言以 select 开头写LINQ查询语法不是没出现过,你如果使用过2005年的VB9 CTP 版本,那时候VB9的LINQ查询语法就是 select 关键字在前面,但是 select 关键字在前面,在做智能感知(Intelisence)时候就很头大。经过微软IDE组的权衡,确定了把 from 关键字放在最前面。

      比如:你看 http://blog.joycode.com/saucer/archive/2005/09/16/63513.aspx 这篇博客,那时候 VB9 LINQ的查询语法还是 select 参数在最前面。不过后来 VB9 测试版改成了跟 C# 一样的做法, from 关键字放在最前面了。

      更详细的解释,来自装配脑袋

      假设你要书写这样的代码:Select p.Name, p.Age From p In persons Where xxx ,代码是一个个字符输入的。

      我们在写到 p in persons 之前,p 的类型是无法推测的,所以写 Select p. 的时候,Name之类的属性不会弹出智能提示来。

      这样就需要先去写 From 这句,再回来写 Select。

      微软IDE组经过反复考虑决定,还不如就把 Select 写到后面了。于是编程语言中的写法就确定这样来写了。

      VB9 的这个变化可以参看这篇博客:

      Select/From vs. From/Select revisited...

      我们再来看一个稍稍复杂的LINQ查询:

      在我们罗列的语言字符串中,我们希望按照字符长短,分类罗列出来,实现代码如下:

      static void Main(string[] args)

      {

      string [] languages = {"Java","C#","C++","Delphi","VB.net","VC.net","C++ Builder","Kylix","Perl","Python"};

      var query = from item in languages

      orderby item

      group item by item.Length into lengthGroups

      orderby lengthGroups.Key descending

      select lengthGroups;

      foreach (var item in query)

      {

      Console.WriteLine("strings of length ",item.Key);

      foreach (var val in item)

      {

      Console.WriteLine(val);

      }

      }

      Console.ReadLine();

      }

      其中的 into 关键字表示 将前一个查询的结果视为后续查询的生成器,这里是跟 group by 一起使用的。

      LINQ中的Group by不要跟 SQL 中的Group by 混淆,SQL 由于是二维结构,Group by 的一些逻辑受二维结构的约束,无法象 LINQ 中的Group by 这么灵活。

    转载于:https://www.cnblogs.com/xbs729/archive/2010/03/27/1698529.html

    展开全文
  • linq语法

    2017-07-25 15:39:00
    LINQPad4.zip------linq转sql文件 一:where 嵌套查询 var q=from p in db.ParkingSpace join uw in db.sys_user_web on p.yardNumber equals uw.yardNumber where (from u in db.sys_user.Where(c => (flag...
  • LINQ 语法

    2014-05-22 00:41:00
    语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中一项突破性的创新,它在对象领域和数据领域之间架起了一座桥梁。 传统上,针对数据的查询都是以简单的字符串表示,而没有编译时类型检查或 ...
  • Linq 语法 总结

    2013-06-17 10:27:29
    Linq 语法 总结
  • LINQ语法基础

    2014-05-15 21:17:48
    LINQ语法基础,文档,语法精确,容易学习
  • LINQ语法与实例

    2011-10-31 10:50:43
    通过实例快速学习LINQ语法.通过实例快速学习LINQ语法.通过实例快速学习LINQ语法.通过实例快速学习LINQ语法
  • asp.net mvc linq语法示例

    2017-03-23 11:24:06
    linq语法示例
  • c# linq语法

    2019-11-11 16:57:33
    namespace linq { class Program { static void Main(string[] args) { //linQ语法 //int[] a = new int[] { 12, 5, 9, 7, 8 }; //var xx = from i in a ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,825
精华内容 1,130
关键字:

linq语法