Lambda表达式
Lambda不使用delegate关键字,而使用Lambda运算符=>
FuncgetInput = (int age) => {
return Console.ReadLine() + age;
}
Entity Framework 实体框架--DataBaseFirst
EF添加数据
UserInfo userInfo = new UserInfo();
userInfo.UserName = "Caty";
EFFristModelEntities db = new EFFristModelEntities ();
db.UserInfo.Add(userInfo);
db.SaveChanges();
EF查询延迟加载
EFFristModelEntities db = new EFFristModelEntities ();
//linq表达式
var userInfoList = from u in db.UserInfo where u.ID == 343 select u;
// EF延迟加载机制,数据用到时才去查询
foreach (UserInfo userInfo in userInfoList)
{
Response Write(userInfo.UserName);
}
删除数据
EFFristModelEntities db = new EFFristModelEntities ();
var userInfoList = from u in db.UserInfo where u.ID == 343 select u;
// 返回第一个元素,如果没有,返回null
UserInfo userInfo = userinfoList.FirstDefault();
if(userInfo != null)
{
db.UserInfo.Remove(userInfo);
//db.Entry.State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
else
{
Resopnse.Write("要删除的数据不存在");
}
更新数据
EFFristModelEntities db = new EFFristModelEntities ();
var userInfoList = from u in db.UserInfo where u.ID == 343 select u;
UserInfo userInfo = userinfoList.FirstDefault();
userInfo.UserName = "小猫";
db.Entry.State = System.Data.EntityState.Modified;
db.SaveChanges();
EF框架--ModelFirst
导航属性应用(添加数据)
Model2Container db = new Model2Container ();
Customer customer = new Customer(){ CustomerName="张三",CustomerPwd="123" };
OrderInfo orderInfo1 = new OrderInfo(){ ID=Guid.NewGuid(),OrderNum = "10001",createDateeTime=Datetime.Now,Customer=customer };
db.Customer.Add(customer);
db.OrderInfo.Add(orderInfo1);
db.saveChanges();
查询数据
Model2Container db = new Model2Container ();
// 查询所有客户的订单
var customerList = from c in db.Customer select c;
foreach(var customer in customerList)
{
Response.Write( customer. CustomerName+":" );
foreach ( var orderInfo in customer.OrderInfo )
{
Response.Write(orderInfo.orderNum+",");
}
}
// 查询单个客户的订单
var customerInfoList = from c in db.Customer where c.ID == 1 select c;
var customerInfo = customerInfoList.FirstOrDefault();
foreach(var orderInfo in customerInfo.OrderInfo)
{
Response.write(orderInfo.OrderNum);
}
EF框架-CodeFirst
/* ClassInfo.cs */
public class ClassInfo
{
[Key] //主键
public int Id { get; set; }
[StringLength(32)]
[Required] //不允许为空
public string ClassName { get; set; }
[Required]
public DateTime CreateTime{ get; set; }
public virtual ICollection StudentInfo { get; set; }
}
/* StudentInfo.cs */
public class StudentInfo
{
[Key] //主键
public int Id { get; set; }
[StringLength(32)]
[Required]
public string StuName { get; set; }
[Required]
public DateTime SubTime{ get; set; }
public virtual ClassInfo ClassInfo { get; set; }
}
/* App.config */
/* CodeFirstDbContext.cs */
public class CodeFirstDbContext:DbContext
{
public CodeFirstDbContext() : base("name=connStr")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//移除复数形式
modelBuilder.conventions.Remove();
}
public DbSet ClassInfo { get; set; }
public DbSet StudentInfo { get; set; }
}
查询部分列
EFFristModelEntities db = new EFFristModelEntities ();
var userInfoList = from u in db.UserInfo where u.ID == 343 select new{UName=u.UserName,UPwd=u.UserPass};
foreach (Var userInfo in userInfoList)
{
Response Write(userInfo.UName+":"+userInfo.UPwd);
}
Lambda查询
EFFristModelEntities db = new EFFristModelEntities ();
// 升序排序, 降序排序用orderByDescending
var userInfoList = db.userInfo.where true).OrderBy(u => u.ID);
//分页 Skip(2)跳过两条记录;Take(2) 取两条记录;
int pageIndex = 2;
int pageSize = 2;
var userInfoList = db.userInfo.where true).OrderBy(u => u.ID).Skip((pageIndex-1)*pageSize).Take(pageSize);
// IQueryable userInfoList = db.userInfo.whereu.ID==343);
线程唯一
EFFirstModelEntities db = null;
if(HttpContext.Current.Items["db"] == null)
{
db = new EFFirstModelEntities();
HttpContext.Current.Items["db"] = db;
}
else
{
db = HttpContext.Current.Items["db"] as EFFristModelEntities;
}