Fork me on GitHub

linq to entity常用操作

一、聚合函数查询

double sum = 0;
using (xxxEntities db = new xxxEntities())
{
    sum = db.userinfo.AsNoTracking().Where(d => d.id==id).Sum(m => (double?)m.money).GetValueOrDefault();
}
return sum;

 

二、删除操作

int result = 0;
if (id > 0)
{
    using (gghdbEntities db = new gghdbEntities())
    {
        var data = db.quote.Where(d => d.id == id).FirstOrDefault();
        db.quote.Remove(data);
        db.SaveChanges();
        result = 1;
    }
}
return result;

 

三、执行事物(Net4.0以上版本支持)

using (xxxEntities db = new xxxEntities())
{
    using (TransactionScope transaction = new TransactionScope())
    {
        //操作,出错自动回滚

        //提交事务
        transaction.Complete();
    }
}

 需要添加引用:using System.Transactions;

 

posted @ 2016-06-09 09:31  磊哥|www.javacn.site  阅读(1040)  评论(0编辑  收藏  举报