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

  • Home
  • SEARCH
  • 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 6546789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:45:05+00:00 2026-05-25T11:45:05+00:00

I have anonymous type variable which i am populating like that(i have two variables

  • 0

I have anonymous type variable which i am populating like that(i have two variables jsonDataCache and jsonData but i want to have just one, jsonData for example) :

 var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,

                rows = (from b in myData
                select new
                        {
                        //do things
                        }).ToArray()
             };
return Json(jsonData, JsonRequestBehavior.AllowGet);

But if i want to define this variable first, before i do populate that, basically i want to do something like that:

var jsonData = null;   

//check if jsonData in cache and if it is return Json(jsonData, JsonRequestBehavior.AllowGet);

jsonData = new
                {
                    total = totalPages,
                    page = page,
                    records = totalRecords,

                    rows = (from b in myData
                    select new
                            {
                            //do things
                            }).ToArray()
                 };
//put jsonData in cache by key

return Json(jsonData, JsonRequestBehavior.AllowGet);

How could i do that?

The reason i want to do that because i want to introduce the cache, so i need to define the variable first ant than check that in cache and if it is not i will do the thing above.
Currently i did that with two variables, bot i want to use just one.

Here is how i currently done that:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonDataCache = CacheManager.Get<object>(keyPrefix);   

    if(jsonDataCache != null)
      return Json(jsonDataCache, JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    var jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

UPDATE:
After all your help I think it should be something like that, hopefully it is right way to go:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonData = CacheManager.Get<object>(keyPrefix);   

    if(jsonData != null)
      return Json(jsonData , JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}               
  • 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-25T11:45:05+00:00Added an answer on May 25, 2026 at 11:45 am

    You can’t do that (and initialize to null), except for some really nasty and ugly “by example” tactics with generic methods. Variables to anonymous types can only be declared alongside initialisation (except as a generic type parameter).

    It does, however, seem unlikely that you really need it here. Indeed,

    object jsonData;
    

    would suffice, since you never use the value. Beyond that, I’d argue that it is time to introduce a DTO class for the purpose instead.

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

Sidebar

Related Questions

I have a function that returns an anonymous type which I want to test
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
I have a Linq query that returns an anonymous type, which I then use
I have a LINQ statement that returns an anonymous type. I need to get
I have an anonymous type in an ASP.NET web form that I'm trying to
I have a LINQ statement that generate an anonymous type, for example: BookID, AuthorID,
I have a MVC3 RouteValueDictionary that I'd like to convert quickly to an anonymous
Is it possible to have an anonymous type implement an interface? I've got a
I have no clue what an anonymous type is in C# nor how it
I have a question of how to sort an anonymous type. Using Linq2SQL I

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.