Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3855200
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:44:22+00:00 2026-05-19T17:44:22+00:00

Using : SQL Server 2008, Entity Framework I am summing columns in a table

  • 0

Using: SQL Server 2008, Entity Framework

I am summing columns in a table across a date/time range. The table is straight-forward:

DataId bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
DateCollected datetime NOT NULL,
Length int NULL,
Width int NULL,
-- etc... (several more measurement variables)

Once I have the date/time range, I use linq-to-EF to get the query back

var query = _context.Data.Where(d =>
                               (d.DateCollected > df &&
                                d.DateCollected < dt))

I then construct my data structure using the sum of the data elements I’m interested in

DataRowSum row = new DataRowSum
{
  Length_Sum = query.Sum(d => d.Length.HasValue ? (long)d.Length.Value : 0),
  Width_Sum = query.Sum(d => d.Width.HasValue ? (long)d.Width.Value : 0),
  // etc... (sum up the rest of the measurement variables)
}

While this works, it results in a lot of DB round trips and is quite slow. Is there a better way to do this? If it means doing it in a stored procedure, that’s fine with me. I just need to get the performance better since we’ll only be adding more measurement variables and this performance issue will just get worse.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:44:22+00:00Added an answer on May 19, 2026 at 5:44 pm

    SQL SERVER is very good at rolling up summary values. Create a proper stored procedure which calculates the sums for you already. This will give you maximum performance, especially if you don’t actually need the tabular data in your client program. Just have SQL Server roll up the summary, and send back a whole lot less data. One of the reasons I generally don’t like LINQ is because it tempts the programmers to do things like what you are trying to do (pull a set and do ‘something’ against every row), instead of taking advantage of the database engine and all its capabilities.

    Do this with aggregate functions and grouping in the SQL. LINQ will never figure out how to do this fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids
I'm using Entity Framework and MS SQL Server 2008. It seems that Entity Framework
Using Entity Framework 4 with stored procedures and SQL Server 2008 SP1... When running
I am using Entity Framework to layer on my SQL Server 2008 database. The
I'm trying to store a document in SQL Server 2008 using the Entity Framework.
I have an MVC app using Entity Framework and a SQL Server 2008 database.
I am using Entity Framework 4.0, C# 4.0, .Net 2010 and SQL Server 2008
Like: insert into table (col) values (N'multilingual unicode strings') I'm using SQL Server 2008
I'm working on a .NET 4 application, C#, Entity Framework 4, SQL Server 2008.
I crated an app with asp.net mvc using entity framework 4 and sql server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.