当前位置: 移动技术网 > IT编程>开发语言>.net > linq2db.EntityFrameworkCore 介绍

linq2db.EntityFrameworkCore 介绍

2018年09月08日  | 移动技术网IT编程  | 我要评论

波拉x,厦门益康医院,燃气壁挂炉品牌

linq2db.entityframeworkcore 是一个ef core的插件,对linq语法的扩展

对复杂的sql都有很好的支持,他是基于linq2db (provided by linq to db)

如果你使用了linq2db的语法扩展那么你必须使用下面的方法进行查询

 // tolinqtodb是必须的
 var temp = qry.tolinqtodb().tolist();

下面是 linq2db 的冰山一角

join

1. innerjoin

 var qry = from t1 in db.t
           from t2 in db.t2.innerjoin(m => m.t1id == t1.id)

2.leftjoin

 var qry = from t1 in db.t
           from t2 in db.t2.leftjoin(m => m.t1id == t1.id)

3.rightjoin

 var qry = from t1 in db.t
           from t2 in db.t2.rightjoin(m => m.t1id == t1.id)

sum

// 相比于原来linq,简洁了很多。
var qry = from t1 in db.t
          from t2 in db.t2.leftjoin(m => m.t1id == t1.id)
          select sql.ext.sum(t1.type == "a" ? t1.number * (t1.saleprice-t2.originalprice) : 0).tovalue();

countext

//我要查t2中不重复的 t1的id有多少个
var qry = from t1 in db.t
          from t2 in db.t2.leftjoin(m => m.t1id == t1.id)
          where t1.some==''
          group new {t1,t2} by t2.some into g
          select new 
          {
              //相当于sql count(distinct t2.t1id)
              number = g.countext(m => m.t2.t1id, sql.aggregatemodifier.distinct)
          }

对于一些sql函数的支持

datepart

var qry = from t1 in db.t
                    where t1.saledate > begintime 
                    group t1 by sql.datepart(sql.dateparts.month, t1 .saledate) into g
                    select new
                    {
                        month = g.key,
                        flowamount = g.sum(m => m.saleway == "a" ? sql.abs(m.number * m.saleprice) : 0) -
                                     g.sum(m => m.saleway == "b" ? sql.abs(m.number * m.saleprice) : 0)
                    };

当然还有更多的扩展方法,分别位于
包含于 sql , sql.ext,analyticfunctions 中
linq2db文档 :

当然还有批量更新的操作

如果是需要使用,那么最好再程序开始时运行以下代码

//因为他是幂等的 ,所以可以多次运行
linqtodbforeftools.initialize();

以下代码都是从github上抄下来的。

// fast insert big recordsets
ctx.bulkcopy(new bulkcopyoptions {...}, items);

// query for retrieving products that do not have duplicates by name
var query =
    from p in ctx.products
    from op in ctx.products.leftjoin(op => op.productid != p.productid && op.name == p.name)
    where sql.tonullable(op.productid) == null
    select p;

// insert these records into the same or another table
query.insert(ctx.products.tolinqtodbtable(), s => new product { name = s.name ... });

// update these records by changing name based on previous value
query.update(prev => new product { name = "u_" + prev.name ... });

// delete records that matched by query
query.delete();

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网