开发者

Sum a column in SQL or C#

开发者 https://www.devze.com 2023-02-05 13:17 出处:网络
I have a question for the TSQL experts out there or the LINQ C# experts out there. I have a stored procedure that joins in a few tables. Basically Name, ID, and about 10 product columns. The stored pr

I have a question for the TSQL experts out there or the LINQ C# experts out there. I have a stored procedure that joins in a few tables. Basically Name, ID, and about 10 product columns. The stored procedure can return a lot of data (90,000+ rows sometimes). In the stored proc I filter the results to filter out all rows where all of the products in that row = 0. What I need to do is to remove columns in C# where all of the values in a column (product) are zero. This is due to some business rules within my application (customer request).

The application can remove (hide) columns by sending an array of the column names to a report builder. For example:

|Name|ID|Prod1|Prod2|Prod3|Prod4|

If I wanted to remove Prod3 and Prod4 from the report:

List<string> hideColumns = new List<string>();
hideColumns.Add("Prod3");
开发者_运维问答hideColumns.Add("Prod4");

and I would send hideColumns.ToArray() to the report builder and those columns would be removed.

In my application I send in all of the data, then use the following LINQ to find the column values for the returned data.

var zeroCols = from result in data
               group result by new { } into C
               select new ProductDataClass
               {
                   Prod1 = C.Sum(x => x.Prod1)
                   Prod2 = C.Sum(x => x.Prod2)
                   ...etc to Prod10
               };

To find which is 0

if (zeroCols.Count() > 0)
{
    if (zeroCols.First().Prod1 == 0)
        hideColumns.Add("Prod1");
    ...etc to Prod10
}

However, my problem is that the LINQ is really slow if the report returns a lot of rows. Can anybody make any suggestions?


It would be faster to split this into two queries- one to return the data, and another aggregate query to return a single row. The return from the aggregate query can be used to determine which columns are 0 and should be hidden.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号