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 7072809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:51:15+00:00 2026-05-28T05:51:15+00:00

I have a LINQ statement which is adding up the values of multiple columns,

  • 0

I have a LINQ statement which is adding up the values of multiple columns, each beginning with ‘HH’ although there are other columns available:

//TODO Clean up this mess
var query1 = (from e in Data
                where e.SD == date
                select e).Select(x =>   x.HH01 + x.HH16 + x.HH17 + x.HH18 + x.HH19 + x.HH20 + x.HH21 + x.HH22 + x.HH23 +
                                        x.HH24 + x.HH25 + x.HH26 + x.HH27 + x.HH28 + x.HH29 + x.HH30 + x.HH31 + x.HH32 + 
                                        x.HH33 + x.HH34 + x.HH35 + x.HH36 + x.HH37 + x.HH38 + x.HH39 + x.HH40 + x.HH41 +
                                        x.HH42 + x.HH43 + x.HH44 +x.HH45 + x.HH46 + x.HH47 + x.HH48 + x.HH49.GetValueOrDefault()+
                                        x.HH50.GetValueOrDefault());

return query1.FirstOrDefault();

Is there any way to tidy this up? I have to do lots of variations of this (in different methods) so it would clear out a lot of ‘fluff’ if it could be.

Also I’d like to call .GetValueOrDefault() on each column, but currently I’ve taken this out due to the mess except for the last two columns.

Suggestions much appreciated!

  • 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-28T05:51:15+00:00Added an answer on May 28, 2026 at 5:51 am

    I guess you can use Reflections for this:

    double GetHHSum<T>(T x) where T : class
    {
       double result = 0;
    
       var properties = typeof(T).GetProperties();
       foreach (var property in properties)
       {
           if (property.Name.StartsWith("HH"))
              sum += Convert.ToSingle(property.GetValue(x)).GetValueOrDefault();
       }
    
       return result;
    }
    

    And then use it like this:

    return (from e in Data
            where e.SD == date
            select e).ToList().Select(x => GetHHSum(x)).FirstOrDefault();
    

    Code is not tested

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

Sidebar

Related Questions

I have the following linq statement which works great, when each gameServer in the
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I'm have a SQL statement which I am trying to transform in a LINQ
I have a linq statement which calls a stored proc and returns a list
I have a LINQ Statement. It searches an XML file. (See example below) There
I have a LINQ statement which is working great... its part of select here
I currently have a fairly long LINQ statement which pulls data from two tables:
I have a LINQ statement which pulls the top N record IDs from a
I have LINQ statement that looks like this: return ( from c in customers
I have a LINQ statement that returns an anonymous type. I need to get

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.