#region linq的标准查询运算符(即lambda方式) 注:C#不支持标准查询运算符中带有整形参数(索引)的重载

// 1、标准查询运算符之筛选方法——where
            //IQueryable<Student> stu1=db.Student.Where(s => s.Ssex == "男");
            //GridView1.DataSource = stu1;
            //GridView1.DataBind();

// 2、标准查询运算符之s投影方法——select

//var stu2 =db.Student.Select(s => new {s.Sno,s.Sname,s.Sage}); //这叫匿名对象
            //GridView2.DataSource = stu2;
            //GridView2.DataBind();

//3、标准查询运算符之排序方法—— order
            //升序排序
            var stu3 = db.Student.OrderBy(s => s.Sage);
            GridView1.DataSource = stu3;
            GridView1.DataBind();

//降序排序之多条件排序
            var stu4 = db.Student.OrderByDescending(s => s.Sage).ThenByDescending(s=>s.Sno);
            GridView2.DataSource = stu4;
            GridView2.DataBind();

    //备注:OrderBy()和OrderDescendingBy()方法不会改变原列表 而是返回排序后的列表 这点是和Sort的不同之处之一。

    //pers.Sort((p1, p2) => p1.Age - p2.Age); //按照Age升序排序

//4、标准运算符之连接集合—— Join

    // 第一个参数:要连接的对象(B)第二个参数:A对象及A对象用来连接的字段;第三个参数:B对象及B对象用来连接的字段;第四个参数:A,B=>返回的对象
            var stu5 = db.Student.Join(db.StuCourse, s => s.Sno, stu => stu.Sno, (s, stu) => new { Sno=s.Sno,SCSno=stu.Sno,s.Sname,stu.Cid});
            GridView2.DataSource = stu5;
            GridView2.DataBind();

//左连接
            var leftJoin = from stu in db.Student
                           join sc in db.StuCourse
                           on stu.Sno equals sc.Sno
                           into temp
                           from t in temp.DefaultIfEmpty()
                           select new { stu.Sno, stu.Sage, Cid = t == null ? 0 : t.Cid };

//右连接
            var right = from sc in db.StuCourse
                        join stu in db.Student
                        on sc.Sno equals stu.Sno
                        into temp
                        from tt in temp.DefaultIfEmpty()
                        select new { sc.Cid, sc.Sno, SName = tt == null ? "" : tt.Sname };

//5、标准运算符之分组——GroupBy
             //按照年龄分组
            var stu6 = db.Student.GroupBy<Student, int>(s => (int)s.Sage);
            List<IGrouping<int, Student>> list2 = stu6.ToList();
            foreach (IGrouping<int,Student> item in list2)
            {
                //输出 小组 的 分组条件
                Response.Write(string.Format("小组:{0}<br/>",item.Key));
                //遍历 小组里 所有的 元素
                foreach (Student st in item)
                {
                    Response.Write(string.Format("姓名:{0}", st.Sname));
                }
                Response.Write("<br/>");
            }

//6、标准运算符之分页数据——Skip + Take
            //假设每页有5条记录
            //  int pageSize = 2;
            //return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var pageOne = db.Student.Skip<Student>(24).Take(6);
            GridView1.DataSource = pageOne;
            GridView1.DataBind();

#endregion

附加:

int[] numbers = { 0, 30, 15, 90, 85, 40, 75,20, };
            int[] arr = { };
            int first = numbers.First<int>(m => m > 35); // 返回满足条件的第一条记录 90
            int last = numbers.Last<int>(n => n < 40);  //返回满足条件的最后一条记录 20
            bool allResult = numbers.All<int>(mm => mm < 100); //判断所有元素是否都满足  true
            bool allResult2 = numbers.All<int>(mm => mm>5); // false
            bool anyResult = numbers.Any(); //numbers中有元素 就返回 true
            bool anyResult1 = arr.Any();    //arr中没有元素 返回false
            bool anyResult2 = numbers.Any<int>(mm => mm <1);  // 只要有一个元素满足条件 就返回true
            bool anyResult3 = numbers.Any<int>(mm => mm >89); //true
            bool anyResult5 = numbers.Any(m => m > 200); //false

Linq使用之标准运算符方法的更多相关文章

  1. SQO (标准查询运算符)方法 & Linq To Object

    #region SQO (标准查询运算符) 方法 #region Where() Find() FindAll() FirstOrDefault()等方法 static void c01where() ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-7  在别的LINQ查询操作中使用Include()方法 问题 你有一个LINQ ...

  3. mvc ef LINQ to Entities 不识别方法“Int32 Parse(System.String)”,因此该方法无法转换为存储表达式。

    private sys_User GetUserInfo() { sys_User model = null; var userId = Convert.ToInt32(AccountHelper.G ...

  4. LINQ to Entities不识别方法***,因此该方法无法转换为存储表达式

    我的程序里有这么一段代码: order.OrderExpressInfo = (from oei in orderExpressRepository.Entities where oei.OrderI ...

  5. LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。

    var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...

  6. Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)

    1.在工作中碰到这样一个问题: 使用linq时,需要查询两个表,在这两张表中关联字段分别是int,和varchar()也就是string,在linq中对这两个字段进行关联, 如果强制类型转换两个不同类 ...

  7. [置顶] IOS7状态栏StatusBar官方标准适配方法

    IOS7状态栏StatusBar官方标准适配方法 hello,大家好,ios7正式版已经发布,相信大家都在以各种方式来适配ios7. 如果你已经下载了xcode5,正准备使用,你会发现各种布局的改变. ...

  8. Linq to Entities不识别方法

    db.UserValidates.Include(a => a.User).Where(uv => u.UserValidates.Contains(uv, c)).ToList(); 执 ...

  9. LINQ to Entities 不识别方法“System.DateTime AddDays(Double)

    今天本想在linq里按照时间筛选一下超时的数据,一共两个字段FeedBackTime(计划反馈时间).EndTime(实际反馈时间).需求是这样的,查找数据库里所有EndTime大于FeedBackT ...

随机推荐

  1. 组策略限制添加用户作为服务登录导致ITAtomcat服务无法启动(log on as a service)

    [故障类型]:ITA tomcat服务器无法启动. [关 键 词]:Logon as a service  作为服务登录  tomcat  loggeter [适用版本]:FusionCloud So ...

  2. thinkphp框架 中 ajax 的应用

    在thinkphp中,内置了ajax的方法,即: ajaxReturn("data","info","status"); data:传递的数 ...

  3. objective-C 自定义对象归档的实现

    自定义对象要实现归档必须实现NSCoding协议 NSCoding协议有两个方法,encodeWithCoder方法对对象的属性数据做编码处理,initWithCoder解码归档数据来初始化对象. # ...

  4. GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告

    实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...

  5. xp主机用VMware9和10安装Ubuntu12.04后无法进入图像界面

    xp主机用VMware9和10安装Ubuntu12.04后无法进入图像界面 备注:虚拟机安装Ubuntu12.04 64位版本 刚开始我用VMware-workstation-full-8.0.3来安 ...

  6. C语言计算程序运行时间

    #include<stdio.h>#include<stdlib.h> #include "time.h" int main( void )  {     ...

  7. UVA 1324 The Largest Clique 最大团(强连通分量,变形)

    题意:给一个有向图,要求找出一些点,使得这些点中的任意点对,要么可以互通,要么单向可达. 思路:最低只要求单向可达即可,即a->b都可以算进去. 强连通分量内的点肯定是满足要求的,可以全选,但是 ...

  8. 图片处理 Pillow

    Pillow 在python3下用PIL做图像处理 Python图像处理库:Pillow 初级教程 from PIL import Image im = Image.open('22.gif') pr ...

  9. (4)java方法区

    java方法区[名词解析]        --->和java堆一样,方法区是一块所有线程共享的内存区域.        --->保存系统的类信息,比如,类的字段,方法,常量池等.      ...

  10. Protobuf语言指南

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...