EF简单的增删改查

  1. JSDEntities edm = new JSDEntities();
  2. //select
  3. var temp = from p in edm.accounts_top_up
  4. where p.ID == 18410
  5. select p;
  6. accounts_top_up atu = temp.Single();
  7. //update
  8. accounts_top_up at = new accounts_top_up() { USER_ID = 18412, ID = 18412 };
  9. DbEntityEntry<accounts_top_up> entry = edm.Entry<accounts_top_up>(at);
  10. entry.State = System.Data.EntityState.Unchanged;
  11. entry.Property("USER_ID").IsModified = true;
  12. edm.SaveChanges();
  13. //add
  14. accounts_top_up a = new accounts_top_up() { USER_ID = 18412, ID = 18412 };
  15. edm.accounts_top_up.Add(a);
  16. edm.SaveChanges();
  17. //delete
  18. edm.Entry<accounts_top_up>(new accounts_top_up() { ID = 18411}).State = EntityState.Deleted
  19. edm.SaveChanges();

2:模糊查询
actualContext.PU.Where(a=>a.ErrorReason.Contains(“sdfsdf”))使用Contains关键字

3: var list = from entity in actualContext.PSI where (string.IsNullOrEmpty(entity.QYPortEN) || entity.QYPortEN == priceinfo.QYPortEN) select entity;

4:where if

where ((sortBy == “Buy equipment”) ? entity.Type == “Sell” : true) && ((sortBy == “Sell equipment”) ? entity.Type == “Buy” : true)
分组查询:

entities db=new entities();

var rslt=from u in tb group u by u.name into g

select new { subject=g.key,average=g.average(u=>u.score),sum=g.sum(a=>a.score)}

var list=rslt.tolist();

分组方法二:

var list=ote.score.GroupBy(a=>a.SubName).Select(a=>new{ subject=a.key,average=a.average(b=>b.score) }).tolist();

发表评论

表情:
评论列表 (有 0 条评论,33人围观)

还没有评论,来说两句吧...

相关阅读