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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:55:42+00:00 2026-06-17T07:55:42+00:00

All I want to do is declare var place correctly so it is still

  • 0

All I want to do is declare var place correctly so it is still in scope once I get to the foreach loop. I’m assuming I need to declare it before the if statement for connections. Is this a correct assumption and if so how do I declare it? Thanks!

using (var db = new DataClasses1DataContext())
        {
            if (connections == "Connections")
            {
                var place = (from v in db.pdx_aparts
                             where v.Latitude != null && v.Region == region && v.WD_Connect >= 1
                             select new
                             {
                                 locName = v.Apartment_complex.Trim().Replace(@"""", ""),
                                 latitude = v.Latitude,
                                 longitude = v.Longitude
                             }).Distinct().ToArray();
            }
            else
            {
                var place = (from v in db.pdx_aparts
                             where v.Latitude != null && v.Region == region &&    ((v.WD_Connect == null) || (v.WD_Connect == 0))
                             select new
                             {
                                 locName = v.Apartment_complex.Trim().Replace(@"""", ""),
                                 latitude = v.Latitude,
                                 longitude = v.Longitude
                             }).Distinct().ToArray();

            }

            foreach (var result in place)
            ....
  • 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-17T07:55:44+00:00Added an answer on June 17, 2026 at 7:55 am

    You can create an array with a single entry whose value you ignore later:

    // Note: names *and types* must match the ones you use later on.
    var place = new[] { new { locName = "", latitude = 0.0, longitude = 0.0 } };
    if (connections = "Connections")
    {
        // Note: not a variable declaration
        place = ...;
    }
    else
    {
        place = ...;
    }
    

    This works because every use of anonymous types using properties with the same names and types, in the same order, will use the same concrete type.

    I think it would be better to make the code only differ in the parts that it needs to though:

    var query = v.db.pdx_aparts.Where(v => v.Latitude != null && v.Region == region);
    query = connections == "Connections"
        ? query.Where(v => v.WD_Connect >= 1)
        : query.Where(v => v.WD_Connect == null || v.WD_Connect == 0);
    var places = query.Select(v =>  new
                              {
                                  locName = v.Apartment_complex
                                             .Trim()
                                             .Replace("\"", ""),
                                  latitude = v.Latitude,
                                  longitude = v.Longitude
                              })
                      .Distinct()
                      .ToArray();
    

    Here it’s much easier to tell that the only part which depends on the connections value is the section of the query which deals with WD_Connect.

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

Sidebar

Related Questions

All I want is to get the website URL. Not the URL as taken
$('#submit').click(function () { //Get the data from all the fields var name = $('input[name=name]');
I want to declare a warning on all fields Annotated with @org.jboss.weld.context.ejb.Ejb in AspectJ.
I want to retrieve declared SVG attributes programmatically. It's all inline content, something like
All I want to do is return the index of the i that is
All I want to do is write hello world to a QGraphicsScene in a
All I want is a pragmatic random number generator in C# so I can
All I want is a one pixel black border around my white UILabel text.
All I want to know is if there's an easy way to add lines
All I want to do is to check if the textfield has been changed.

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.