简体   繁体   English

C#SQL / Linq / Entity Framework计算大型数据源中多个列的列总数

[英]C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source

Sorry to bother you, however I'm having issues converting my SQL Query into C# Entity Framework. 抱歉打扰您,但是将我的SQL查询转换为C#实体框架时遇到问题。

My SQL query is as follows: 我的SQL查询如下:

SELECT CAST(ROUND(sum(size/rate), 0) AS INT) s,
CAST(ROUND(sum(PL/rate), 0) AS INT) PL
FROM [bs].[b] b
join [bs].[s] s on b.id = s.b_id
join [bs].[o] o on s.o_id = o.id
join [bs].[a] a on o.a_id = a.id
join [fs].[f] f on b.f_id = f.id


where f.r_date 
between '2013-05-01 00:00:00.000'
and '2013-05-31 00:00:00.000'
and s.deleted_at is NULL
and b.group_id = '0'
and (o.a_id = 50 or o.a_id = 52)

I have in turn managed to get all the joins done and where statement in place (aka 'The Easy Bit') however I just cannot find a way to get those sums for the column totals to work. 反过来,我设法完成了所有的连接,并在where语句中放置了适当的位置(又名“易位”),但是我只是找不到一种方法来获取这些总和以使列总计起作用。

This is what I have in place so far: 到目前为止,这是我所拥有的:

var GroupSk = (from Bs in sb.b
join S in sb.s on Bs.id equals S.b_id
join O in sb.o on S.o_id equals O.id
join A in sb.a on O.a_id equals A.id
join Fs in sb.vw_f on Bs.f_id equals Fs.f_id

where Fs.r_date >= t_FromDate && Fs.r_date <= t_ToDate
where S.deleted_at == null
where Bs.group_id == 0
where O.a_id == 50 || O.a_id == 52

select new {

As you can see, it's everything up until the SUM part of the query. 正如您所看到的,直到查询的SUM部分为止,一切都是如此。

This query can return anywhere from 1-150000 rows, and I need a way to ensure that the column totals I get back are returned in a timely manner. 该查询可以返回1-150000行中的任何位置,并且我需要一种方法来确保及时返回我返回的列总数。

I had originally planned on using a ForEach loop but had trouble implementing it (along with the fact that it'll probably take a LONG time if a larger number of rows are returned). 我原本打算使用ForEach循环,但是在实现它时遇到了麻烦(以及这样的事实:如果返回大量行,可能会花费很长时间)。

I'm aware there are a few 'sum column total' questions out there, however they don't deal with multiple tables and multiple column outputs. 我知道那里有一些“总列总计”问题,但是它们不处理多个表和多个列输出。 They also appear to be limited to 2 or 3 columns total, whereas my tables far exceed that. 它们似乎也被限制为总共2或3列,而我的表却远远超过了。

Any & all help would be greatly appreciated. 任何及所有帮助将不胜感激。

It's a bit of a hack, but it works. 这有点hack,但是可以用。 The trick is to make one group containing all items and then do the sums over the group: 诀窍是使一个包含所有项目的组,然后对该组求和:

var GroupSk = (from Bs in sb.b
    join S in sb.s on Bs.id equals S.b_id
    join O in sb.o on S.o_id equals O.id
    join A in sb.a on O.a_id equals A.id
    join Fs in sb.vw_f on Bs.f_id equals Fs.f_id

    where Fs.r_date >= t_FromDate && Fs.r_date <= t_ToDate
    where S.deleted_at == null
    where Bs.group_id == 0
    where O.a_id == 50 || O.a_id == 52

    select new { r1 = ??.size / ??.rate, r2 = ??.PL / ??.rate })
    .GroupBy(x => 0)
    .Select(g => new { 
                        R1 = g.Sum(x => x.r1), 
                        R2 = g.Sum(x => x.r2)
                     });

I put ?? 我放?? marks where I didn't know the origin of the properties, so you'll have to substitute the right variable names there. 标记我不知道属性来源的位置,因此您必须在此处替换正确的变量名称。 ( Bs, S, O, A, Fs ). Bs, S, O, A, Fs )。

This will translate into one SQL query, so all the processing is done by the database engine and only the small result object is transferred over the wire. 这将转换为一个SQL查询,因此所有处理都由数据库引擎完成,并且只有较小的结果对象通过网络进行传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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