简体   繁体   中英

Is there a linq to entities equivalent for the LAG() function in SQL?

I created a query for a Month-Over-Month Sales report using the LAG function in SQL. I discovered that is not available in linq. Is there a way to still accomplish this?

LinqTo2Db supports Window-Functions Lag and Lead :

from x in db.Events
let prev = Sql.Ext
                .Lag(x.app, Sql.Nulls.None)
                .Over()
                .OrderBy(x.time)
                .ToValue()
where  (prev != x.app && prev != null)
select (prev, x.app)

First 8 of all 43 supported funcs (!)

|-------------|----------------------| 
|    SQL      |       Linq2db        |
|-------------|----------------------| 
| AVG         | Sql.Ext.Average()    |
| CORR        | Sql.Ext.Corr()       |
| COUNT       | Sql.Ext.Count()      |
| COVAR_POP   | Sql.Ext.CovarPop()   |
| COVAR_SAMP  | Sql.Ext.CovarSamp()  |
| CUME_DIST   | Sql.Ext.CumeDist()   |
| DENSE_RANK  | Sql.Ext.DenseRank()  |
| FIRST_VALUE | Sql.Ext.FirstValue() |
|-------------|----------------------|

Note There is no limitation in window functions usage. LINQ To DB will create SQL and run query, if function is not supported or some part of function is limited in particular Database - error will be thrown on database side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM