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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:04:08+00:00 2026-06-12T22:04:08+00:00

I am building a JSON string and in the rows section, I have two

  • 0

I am building a JSON string and in the rows section, I have two values that I want to build and pass in the JSON string which are calculated by a helper function. I am wondering if there a way to call this helper function once, and return back an array of values (two in my case) so I don’t have to call the helper function twice (and avoid hitting the database twice).

Example Code

            rows = (
                from tempItem in pagedQuery.ToList()
                select new
                {
                    cell = new string[] {                    
                        tempItem.Name,
                        tempItem.Regular,
                        HelperFunction.GetPrice(tempItem.ID, false).ToString(),
                        tempItem.Premium,
                        HelperFunction.GetPrice(tempItem.ID, true).ToString(),
                    }
                }).ToArray()

Example Function:

public decimal GetPrice(int ID, bool Premium)
{
  Item item = databaseCallToGetPrice(ID).first();

  if (Premium)
      return item.ExamplePrice;
  else
      return item.PremiumExamplePrice;
}

So what I am asking is in my example I call the Helper function twice, is there a way to call on it just the once and then return back an array that I can somehow persist and then use twice.

  • 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-06-12T22:04:09+00:00Added an answer on June 12, 2026 at 10:04 pm

    Return a Tuple<decimal, decimal> from the function:

    public Tuple<decimal, decimal> GetPrices(int ID)
    {
        Item item = databaseCallToGetPrice(ID).First();
        return Tuple.Create(item.ExamplePrice, item.PremiumExamplePrice);
    }
    

    Then use that:

    rows = (
            from tempItem in pagedQuery.ToList()
            let prices = HelpererFunction.GetPrices(tempItem.ID)
            select new
            {
                cell = new string[] {                    
                    tempItem.Name,
                    tempItem.Regular,
                    prices.Item1.ToString(),
                    tempItem.Premium,
                    prices.Item2.ToString(),
                }
            }).ToArray()
    

    IMHO a tuple is better than a simple array because you’re guaranteed to get two and only two items back from the function.


    If you want to be even clearer, you can create a struct

    public struct Prices
    {
        public decimal PremiumPrice, Price;       
    
        public Prices(decimal premium, decimal price)
        {
            PremiumPrice = premium;
            Price = price;
        } 
    }
    

    then return that from the helper function

    public Prices GetPrices(int ID)
    {
        Item item = databaseCallToGetPrice(ID).First();
        return new Prices(item.PremiumExamplePrice, item.ExamplePrice);
    }
    

    and use that:

    rows = (
            from tempItem in pagedQuery.ToList()
            let prices = HelpererFunction.GetPrices(tempItem.ID)
            select new
            {
                cell = new string[] {                    
                    tempItem.Name,
                    tempItem.Regular,
                    prices.Price.ToString(),
                    tempItem.Premium,
                    prices.PremiumPrice.ToString(),
                }
            }).ToArray()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a JSON string programmatically. The end product should be something
I have some JSON that looks like: groups: [ group_id: 8, group_name: Building, group_color:
I have build a JSON string (to be posted to a web service), and
I am building an Android application and I have JSON data which contains Unicode
I have a stream of JSON coming back and am building a table dynamically
I'm building an API that will use JSON. The primary use for this API
So I'm building a web application and I have an ajax request that pings
I'm building a small application that pulls statistics from an API I have no
I'm building an application that communicates with a django backend using json-rpc. So far
I am building a Sencha touch 2 app. I have a store that gets

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.