DigitalPlatform / dp2

Integrated Library System / 图书馆集成系统
http://digitalplatform.github.io/dp2
Apache License 2.0
106 stars 54 forks source link

新版报表模块 #627

Open DigitalPlatform opened 4 years ago

DigitalPlatform commented 4 years ago

新版报表模块试图解决 dp2library 服务器端无法生成报表的问题。

新版函数库可以被 dp2library.exe 调用,也可以被前端调用。

DigitalPlatform commented 4 years ago

主要的 EF Query 注释

EF Core 3 要求 GroupBy 后面跟随适当的 Select,才能转换为 SQL 服务器端调用

            var opers = context.CircuOpers
            .Where(b => (strLibraryCode == "*" || b.LibraryCode == strLibraryCode)
            && string.Compare(b.Date, strStartDate) >= 0
            && string.Compare(b.Date, strEndDate) <= 0)
            .Join(
                context.Patrons,
                oper => oper.ReaderBarcode,
                patron => patron.Barcode,
                (oper, patron) => new
                {
                    Department = patron.Department,
                    BorrowCount = oper.Action == "borrow" ? 1 : 0,
                    ReturnCount = oper.Action == "return" ? 1 : 0
                }
            )
            .GroupBy(x => x.Department)
            .Select(g => new
            {
                Department = g.Key,
                BorrowCount = g.Sum(t => t.BorrowCount),
                ReturnCount = g.Sum(t => t.ReturnCount)
            })
            .OrderByDescending(t => t.BorrowCount).ThenBy(t => t.Department)
            .ToList();

注意上面的 GroupBy 后面必须跟上 Select,而且 Select 中应该是一个 key 加上若干个 Sum 列。如果不满足这个条件,就会抛出异常(然后只能在 GroupBy 前面加上一个 AsEnumberable() (转换为 Client 端 LINQ)才行了),但 Client 端 LINQ 对资源消耗很大。

参考文献:

https://stackoverflow.com/questions/59401492/entityframework-core-3-linq-expression-could-not-be-translated

https://stackoverflow.com/questions/22932951/how-to-add-sum-aggregate-function-into-entity-framework-query

https://www.thinktecture.com/entity-framework/entity-framework-core-30-hidden-group-by-capabilities-part-1/

https://github.com/dotnet/efcore/issues/18102 https://github.com/dotnet/efcore/issues/17068

DigitalPlatform commented 4 years ago

https://docs.microsoft.com/en-us/ef/core/saving/basic

https://entityframeworkcore.com/zh-CN/knowledge-base/55766322/

https://www.connectionstrings.com/mysql/

Keep TCP Sockets alive
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Keepalive=10;
Number of seconds between each keep-alive package send.

https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host

https://stackoverflow.com/questions/815586/using-transactions-or-savechangesfalse-and-acceptallchanges

https://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx

https://www.red-gate.com/simple-talk/dotnet/net-framework/catching-bad-data-in-entity-framework/

https://stackoverflow.com/questions/45723807/entity-framework-with-mysql-throws-packets-larger-than-max-allowed-packet-are-n

https://github.com/MySqlBackupNET/MySqlBackup.Net/wiki/Packets-larger-than-max_allowed_packet-are-not-allowed

https://www.codeproject.com/Articles/1164269/LINQ-to-Entities-Cross-Apply-and-Left-Outer-Join

DigitalPlatform commented 4 years ago

https://stackoverflow.com/questions/5537995/entity-framework-left-join