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.简单的函数计算(countminmaxsum

//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 语法举例的更多相关文章

  1. Linq语法详细

    闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以进右上角群,进行交流探讨,谢谢. 开门见山 读这篇文章之前,我先说下,每一种搜索 ...

  2. Linq语法详细(转)

    原文地址:http://www.cnblogs.com/knowledgesea/p/3897665.html 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的 ...

  3. Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式

    当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...

  4. Linq语法

    希望能帮助一些linq新手. 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦. 1.简单的linq语法 //1 var ss = fro ...

  5. Lamda和Linq语法对比详细

    本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...

  6. LinQ 语法基础

    LINQ (Language-Integrated Query,语言集成查询). LINQ to Objects.LINQ to SQL.LINQ to DataSet和LINQ to XML,它们分 ...

  7. Linq语法常见普通语法

    闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以进右上角群,进行交流探讨,谢谢. 开门见山 读这篇文章之前,我先说下,每一种搜索 ...

  8. C# LINQ语法详解

    1.简单的linq语法 var ss = from r in db.Am_recProScheme select r; var ss1 = db.Am_recProScheme; string sss ...

  9. LINQ语法类似于SQL的语法

    LINQ语法类似于SQL的语法如下, Models.BookStoreEntities 是从添加新建项中的数据--->ADO.NET实体数据模型--->从数据库生成--->使用5.0 ...

随机推荐

  1. Rx 入门 示例

    首先写一个观察者模式 public interface Watcher { public void update(String str); } public class ConcreteWatcher ...

  2. codevs2492上帝造题的七分钟 2(线段树)

    /* 区间修改 区间查询 可以用线段树搞 但是一般的标记下放对这个题好像不合适 只能改叶子 然后更新父亲(虽然跑的有点慢) 小优化:如果某个点是1 就不用再开方了 所以搞一个f[i]标记 i 这个点还 ...

  3. java 跳转地址栏地址改变

    在strtus1 中,很多都是直接的action 配置后进行跳转的 这样地址栏是不会改变的 如果需要进行浏览器跳转 ActionForward actionForward = new ActionFo ...

  4. IE浏览器中设为首页

    <a onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('<%=Configurati ...

  5. [转] iOS TableViewCell 动态调整高度

    原文: http://blog.csdn.net/crayondeng/article/details/8899577 最近遇到了一个cell高度变化的问题,在找解决办法的时候,参考了这篇文章,觉得不 ...

  6. Ubuntu下管理员界面的切换

    不同于redhat的Linux系统可以通过命令su root 简单地切换到管理员状态,ubuntn 默认是没有超级用户的,因此如需使用管理员权限可以使用以下2种方法: 1.sudo -i 此命令不需要 ...

  7. TCP/IP-入门

    Life is not a rehearsal "人生没有彩排" 参考资料:TCP/IP入门经典 (第五版) TCP/IP详解 卷一:协议 一.什么是TCP/IP TCP/IP是一 ...

  8. c++builder向c#开发的webservice传递非数字参数

    一.引用WebService地址 BCB6.0环境下,File-New-Other-WebService-WSDL Importer.然后手动写完整地址.如:“http://192.168.1.3:1 ...

  9. Ecshop后台菜单添加

    首先需要修改四个文件:inc_priv.php, inc_menu.php, priv_action.php, commn.php 假如当前的项要加在商品管理的菜单下 一:在languages/zh_ ...

  10. A题

    A - A Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u   Descriptio ...