• EF写统计


    EF的特性是,你from的第一个表为主表,接下来的所有表以左联或者内联或者交叉连接的方式去显示,不会出现右联,

    在编写的时候,可以先确定个数据源,然后对这个数据源进行数据的统计,

    例如SQL:

    -- Region Parameters
    DECLARE @p0 VarChar(1000) = 'ExamImage'
    DECLARE @p1 VarChar(1000) = ''
    DECLARE @p2 Float = 1024
    DECLARE @p3 Float = 1024
    -- EndRegion
    SELECT SUM(((CONVERT(Float,[t6].[value])) / @p2) / @p3) AS [FileSize], MAX([t6].[value2]) AS [UploadTime], [t6].[ResultDate]
    FROM (
        SELECT [t3].[FileSize] AS [value], [t3].[UploadTime] AS [value2], [t0].[ResultDate], [t3].[TypeCode], [t0].[ServiceSectID], [t3].[DeleteFlag]
        FROM [a] AS [t0]
        LEFT OUTER JOIN [b] AS [t1] ON [t0].[VisitUID] = ([t1].[VisitUID])
        LEFT OUTER JOIN [c] AS [t2] ON ([t0].[PatientID] = [t2].[PatientID]) AND ([t0].[PatientMasterID] = [t2].[PatientMasterID])
        LEFT OUTER JOIN [d] AS [t3] ON (CONVERT(NVarChar(MAX),[t0].[ObservationUID])) = [t3].[BusinessID]
        LEFT OUTER JOIN [e] AS [t4] ON [t3].[FileUID] = [t4].[FileUID]
        LEFT OUTER JOIN [f] AS [t5] ON [t0].[OrganizationID] = [t5].[OrganizationID]
        ) AS [t6]
    WHERE ([t6].[TypeCode] = @p0) AND ([t6].[ServiceSectID] <> @p1) AND (NOT ([t6].[DeleteFlag] = 1))
    GROUP BY [t6].[ResultDate]

    翻译成linq可以进行如下写法:

     from item in
                            (
                                from o in a
                                join v in b on o.VisitUID equals v.VisitUID into o_vJoin
                                from o_v in o_vJoin.DefaultIfEmpty()
                                join p in b on new { PatientID = o.PatientID, PatientMasterID = o.PatientMasterID } equals new { p.PatientID, p.PatientMasterID } into o_pJoin
                                from o_v_p in o_pJoin.DefaultIfEmpty()
                                join d in c on o.ObservationUID.ToString() equals d.BusinessID into o_dJoin
                                from o_v_p_d in o_dJoin.DefaultIfEmpty()
                                join dis in d on o_v_p_d.FileUID equals dis.FileUID into d_disJoin
                                from o_v_p_d_dis in d_disJoin.DefaultIfEmpty()
                                join om in e on o.OrganizationID equals om.OrganizationID into o_omJoin
                                from o_v_p_d_dis_om in o_omJoin.DefaultIfEmpty()
                                where o_v_p_d.TypeCode == "ExamImage" && o.ServiceSectID != "" && !o_v_p_d.DeleteFlag
                                select new
                                {
                                    FileSize = o_v_p_d.FileSize,
                                    UploadTime = o_v_p_d.UploadTime,
                                    ResultDate = o.ResultDate
                                }
                            )
                        group item by new { item.ResultDate} into res
                        select new
                        {
                            FileSize = res.Sum(t => t.FileSize/1024d/1024d),
                            UploadTime = res.Max(t => t.UploadTime),
                            ResultDate = res.Select(t => t.ResultDate),
                        }

    如果需要映射到一个已有的实体,可以进行如下写法:

      var classInfoSql = getBasicDataByOrderSql.ToList().Select(s => new ClassInfo
                    {
                        FileSize = s.FileSize,
                        OrganizationID = s.OrganizationID
                    });

    其中 

    getBasicDataByOrderSql
    就是上面分组后的linq语句

    这其中涉及到了多表连接,以及使用聚合函数对列进行统计的语法,希望对大家有帮助

  • 相关阅读:
    [原创] 扩展jquery-treegrid插件, 实现勾选功能和全删按钮.
    [原创]多版本Java环境变量的配置
    [转]Redmine 配置163邮箱
    [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server
    服务器控件中使用<%#...>, JS和html控件中使用<%=...>
    【字源大挪移—读书笔记】 第三部分:字尾
    【字源大挪移—读书笔记】 第二部分:字根
    使用WebClient 或者 HttpWebRequest均报:"The Remote name can't be solved"
    【字源大挪移—读书笔记】 第一部分:字首
    【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】
  • 原文地址:https://www.cnblogs.com/llcdbk/p/6212086.html
Copyright © 2020-2023  润新知