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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:41:08+00:00 2026-05-20T15:41:08+00:00

I have an asp.net page (C# codebehind), and in page_load there are various linq

  • 0

I have an asp.net page (C# codebehind), and in page_load there are various linq queries returning anonymous types, such as:

var customersAdded = from r in AddedRecords
                                 select new { Ref = r.CustomerRef, Name = r.Customer_Name };

On the page I have a button called ‘export to excel’, and in that buttonClick event I try to refer to customersAdded. It says it doesn’t exist in the context. I know I’ve overcome similar issues in the past using things along the lines of:

if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/App_Variants/" + GetUserTheme().ToString() + "/images/" + i.ImageUrl)))

But haven’t had luck so far.

So question is, how to refer to the variable? And for reference, if I say wanted to refer to a value in a control (eg a textbox on the page) would the same or different approach apply?

Thanks!

Mark

  • 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-20T15:41:09+00:00Added an answer on May 20, 2026 at 3:41 pm

    It looks like you just declared your variable as a local variable to Page_Load. To refer to the variable in any other method, it would at least need to be a member of the class. Of course, you’re using an anonymous type, and the type itself also needs to be in scope.

    One way to address the issue would be to declare the type within the class, probably just as an inner struct. Then, the type is no longer anonymous, so you’re free to declare a class member as a collection of your new type. You can assign to that variable in Page_Load, then access the variable elsewhere in your class.

    Edit:

    The variety of “lazy-loading” I am referring to in my comment is not the language feature with the lazy keyword, nor anything involving yield, etc., but rather just this:

    private IQueryable<AddedRecord> _customersAdded;
    
    // make this public if you want to access outside the class
    private IQueryable<AddedRecord> CustomersAdded
    {
        get
        {
            if (_customersAdded == null)
            {
                // replace next statement with the query you need
                _customersAdded = AddedRecords.Where(something);
            }
            return _customersAdded;
        }
    }
    

    Now, if you need more parameters to make this query happen, you can always populate _customersAdded ahead of time. This is kind of a trivial example, admittedly.

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

Sidebar

Related Questions

I have an asp.net page with three ajax updatepanels. in each panel there are
I have developed a web application in asp.net, there is a page in this
I have a static connection variable in an ASP.NET webform codebehind. In the page
I have an ASP.Net page that has a script:# var Show; $(document).ready(function () {
I have an ASP.NET page with code-behind in VB.NET. On the ASPX page I
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
NET and VB.net code behind. I have a classic ASP page that connects to
I have ASP.NET page with an iframe on it for displaying some pdf reports
I have ASP.NET page, it calls webservice via $.ajax and returns result in json
I have this serious: I have ASP.NET page, This page contents Update panel with

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.