]Linq to EF 增删改查

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();
}
}
}

  

转载于:https://www.cnblogs.com/Akgu/p/5155101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值