如何优化我的linq

时间:2014-03-05 08:50:41

标签: c# linq-to-sql

 List<string> res = new List<string>();
                var all = data.Select(c => new 
                {
                    All = db.Costs.Where(d => d.IdInformation == c.Id).DefaultIfEmpty().Select(f => new 
                    {
                        Summ = f.All.HasValue ? f.All.Value : 0,
                        BA = f.BA.HasValue ? f.BA.Value : 0,
                        BK = f.BK.HasValue ? f.BK.Value : 0,
                        BT = f.BT.HasValue ? f.BT.Value : 0,
                        Cash = f.Cash.HasValue ? f.Cash.Value : 0
                    })
                }).Select(g => new
                   {
                       Summ1 = g.All.Sum(h => h.Summ)/g.All.Count(),
                       Nad1 = g.All.Sum(j => j.BA+j.BK+j.BT+j.Cash),
                   });
                var summ = all.Sum(c => c.Summ1);
                var nad = all.Sum(c => c.Nad1);
                res.Add(summ.ToString());
                res.Add(nad.ToString());
                res.Add((summ-nad).ToString());
                return res;

我的查询很长时间了,如何优化这个? 数据是可以查询的 请帮忙 我的查询很长时间,如何优化这个? 数据是可以查询的 请帮忙

{SELECT ((
    SELECT SUM([t4].[value])
    FROM (
        SELECT 
            (CASE 
                WHEN [t3].[All] IS NOT NULL THEN [t3].[All]
                ELSE @p2
             END) AS [value]
        FROM (
            SELECT NULL AS [EMPTY]
            ) AS [t1]
        LEFT OUTER JOIN (
            SELECT [t2].[All]
            FROM [dbo].[Costs] AS [t2]
            WHERE [t2].[IdInformation] = ([t0].[Id])
            ) AS [t3] ON 1=1 
        ) AS [t4]
    )) / (CONVERT(Float,(
    SELECT COUNT(*)
    FROM (
        SELECT NULL AS [EMPTY]
        ) AS [t5]
    LEFT OUTER JOIN (
        SELECT NULL AS [EMPTY]
        FROM [dbo].[Costs] AS [t6]
        WHERE [t6].[IdInformation] = ([t0].[Id])
        ) AS [t7] ON 1=1 
    ))) AS [Summ1], (
    SELECT SUM([t11].[value] + [t11].[value2] + [t11].[value3] + [t11].[value4])
    FROM (
        SELECT 
            (CASE 
                WHEN [t10].[BA] IS NOT NULL THEN [t10].[BA]
                ELSE @p3
             END) AS [value], 
            (CASE 
                WHEN [t10].[BK] IS NOT NULL THEN [t10].[BK]
                ELSE @p4
             END) AS [value2], 
            (CASE 
                WHEN [t10].[BT] IS NOT NULL THEN [t10].[BT]
                ELSE @p5
             END) AS [value3], 
            (CASE 
                WHEN [t10].[Cash] IS NOT NULL THEN [t10].[Cash]
                ELSE @p6
             END) AS [value4]
        FROM (
            SELECT NULL AS [EMPTY]
            ) AS [t8]
        LEFT OUTER JOIN (
            SELECT [t9].[BT], [t9].[BA], [t9].[BK], [t9].[Cash]
            FROM [dbo].[Costs] AS [t9]
            WHERE [t9].[IdInformation] = ([t0].[Id])
            ) AS [t10] ON 1=1 
        ) AS [t11]
    ) AS [Nad1]
FROM [dbo].[Information] AS [t0]
WHERE (CONVERT(NVarChar,DATEPART(Year, [t0].[DateAdd]))) IN (@p0, @p1)
}

这个sql生成了我的代码,我使用linq-to-sql

0 个答案:

没有答案