using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//创建数据库实体
ModelStudentContainer DemoDB = new ModelStudentContainer(); #region 增加数据 ////创建具体实体(表中一行)
//Student stu1 = new Student()
//{
// ID = 1,
// Name = "张飞",
// Age = 20
//};
//Student stu2 = new Student()
//{
// ID = 2,
// Name = "关羽",
// Age = 21
//};
//Student stu3 = new Student()
//{
// ID = 3,
// Name = "赵云",
// Age = 22
//};
//Student stu4 = new Student()
//{
// ID = 4,
// Name = "刘备",
// Age = 23
//};
//Student stu5 = new Student()
//{
// ID = 5,
// Name = "刘表",
// Age = 24
//};
//Student stu6 = new Student()
//{
// ID = 6,
// Name = "刘禅",
// Age = 25
//}; ////将实体stu添加到数据库实体对象中
//DemoDB.Student.AddObject(stu1);
//DemoDB.Student.AddObject(stu2);
//DemoDB.Student.AddObject(stu3);
//DemoDB.Student.AddObject(stu4);
//DemoDB.Student.AddObject(stu5);
//DemoDB.Student.AddObject(stu6); ////保存对数据库的修改
//DemoDB.SaveChanges(); #endregion #region 查询1:一般查询,查询所有数据 //var result = from u in DemoDB.Student
// select u; //foreach (var item in result)
//{
// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);
//} #endregion #region 查询2:使用ToList防止延迟加载,拆分子查询 ////这个查询结果可以保存到服务器内存中
//var result1 = (from u in DemoDB.Student
// where u.Age >= 22
// select u).ToList<Student>(); //var result2 = from u in result1
// where u.Name != "赵云"
// select u; //foreach (var item in result2)
//{
// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);
//} #endregion #region 查询3:使用Where方法(lambda表达式) ////使用了Where泛型方法,要传入一个lambda表达式
//var result = DemoDB.Student
// .Where<Student>(t => t.ID == 1); //foreach (var item in result)
//{
// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);
//} #endregion #region 查询4:Linq分页查询 ////要跳过多少页
//int skipPage;
////每页两条数据
//int countPerPage=2; ////从跳过0页到跳过2页(输出第1-3页)
//for (skipPage = 0; skipPage <= 2; skipPage++)
//{
// var result = DemoDB.Student
////分页必须排序
// .OrderBy(t => t.ID)
////跳过指定页数
// .Skip<Student>(skipPage * countPerPage)
////获取条数
// .Take<Student>(countPerPage); // Console.WriteLine("第{0}页:", skipPage + 1);
////输出查询结果(该页)
// foreach (var item in result)
// {
// Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);
// } // Console.WriteLine();
//} #endregion #region 查询5:使用匿名类查询多个字段 //var result = from p in DemoDB.Student
// where p.ID < 4
////定义一个包含了ID和Name信息的匿名类
// select new { p.ID, p.Name }; //foreach (var item in result)
//{
////这里无法输出年龄
// Console.WriteLine("ID:{0},姓名:{1}。", item.ID, item.Name);
//} #endregion #region 修改1:修改1条记录 ////这里没有写where字句,会查询出4条记录
//var result = from u in DemoDB.Student
// select u; ////但是这个方法一次只能取出一条记录
////注:First方法如果返回空值则抛异常
//var target = result.FirstOrDefault<Student>();
////所以只修改了查询结果集中的第一条记录
//target.Name = "司马懿"; //DemoDB.SaveChanges(); #endregion #region 修改2:修改多条记录 //var result = from u in DemoDB.Student
// select u; ////使用循环方式,修改所有查询到的记录
//foreach (var item in result)
//{
// item.Age = 10;
//} //DemoDB.SaveChanges(); #endregion #region 删除 //var result = from u in DemoDB.Student
// where u.Name == "关羽"
// select u; ////删除所有查询结果(这里只有一条结果)
//foreach (var item in result)
//{
// DemoDB.Student.DeleteObject(item);
//} //DemoDB.SaveChanges(); #endregion Console.WriteLine("执行完毕!");
Console.ReadKey();
}
}
}

  

]Linq to EF 增删改查的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间的关系 ...

  2. 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序

    分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图.      1.首先建立一个数 ...

  3. WPF MVVM+EF增删改查 简单示例(二) 1对1 映射

    WPF MVVM+EF增删改查 简单示例(一)实现了对学生信息的管理. 现在需求发生变更,在录入学生资料的时候同时需要录入学生的图片信息,并且一名学生只能有一张图片资料.并可对学生的图片资料进行更新. ...

  4. C# EF增删改查

    1.增 //1.创建一个EF数据上下文对象 MyDBEntities context=new MyDBEntities(); //2.将要添加的数据,封装成对象 Users user = new Us ...

  5. MVC3.0 EF增删改查的封装类

    本人亲身使用EF CodeFirst,因为增删改查都是使用EF内置的一些方法,我想把它封装到一个类调用就行了.结合网上的资料和自己的整理,若有不对的地方望斧正,感激不尽.直接上代码吧.我就用新闻的增删 ...

  6. linq的简单增删改查

    Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...

  7. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  8. Linq to sql 增删改查(转帖)

    http://blog.csdn.net/pan_junbiao/article/details/7015633   (LINQ To SQL 语法及实例大全) 代码 Code highlightin ...

  9. EF学习笔记-1 EF增删改查

    首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...

随机推荐

  1. intellij idea使用技巧

    1 小窗口脱离了主窗口的解决办法 只要将floating mode和windowed mode取消掉就可以了,当选择上了floating mode和windowed mode之后会打一个勾,再次点击勾 ...

  2. Linux就该这么学--命令集合10(vim编辑器)

    1.vim编辑器的命令模式中常用的快捷键: dd 删除(剪切)光标所在整行 5dd 删除(剪切)从光标处开始的5行 yy 复制光标所在整行 5yy 复制从光标处开始的5行 p 将之前删除(dd)或复制 ...

  3. maven采用tomcat7启动项目

    1.maven 集成tomcat7启动项目 <plugin> <groupId>org.apache.tomcat.maven</groupId> <arti ...

  4. Windows Server 2008 MS Office 操作 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))。

    Make sure that you have Office runtime installed on the server. If you are using Windows Server 2008 ...

  5. 51Nod 1515 明辨是非 —— 并查集 + 启发式合并

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非  题目来源: 原创 基准时间限制:1 ...

  6. html5--1.10绝对路径和相对路径

    html5--1.10绝对路径和相对路径 学习要点: 绝对路径和相对路径 1.绝对路径 需要指出链接资源的绝对位置,与你的HTML文档的位置无关: 1. 服务器中的位置:href="http ...

  7. Meta viewport 学习整理

    The meta viewport tag contains instructions to the browser in the matter of viewports and zooming. I ...

  8. leetcode 303. Range Sum Query - Immutable(前缀和)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. javacpp-FFmpeg系列补充:FFmpeg解决avformat_find_stream_info检索时间过长问题

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  10. python爬虫知识点总结(四)Requests库的基本使用

    官方文档:http://docs.python-requests.org/en/master 安装方法 命令行下输入:pip3 install requests.详见:https://www.cnbl ...