Sealer_05
Sealer_05

Reputation: 5576

Declaring anonymous type as array correctly to keep scope

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)
            ....

Upvotes: 3

Views: 673

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1503489

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.

Upvotes: 7

sa_ddam213
sa_ddam213

Reputation: 43636

you could just use the 1 query since they are pretty much the same, and just add the extra condition in the where clause

var place = (from v in db.pdx_aparts
             where v.Latitude != null && v.Region == region 
             &&  connections == "Connections" 
             ? v.WD_Connect >= 1 
             : ((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)
 ....

Upvotes: 1

usr
usr

Reputation: 171246

You could convert the if to a ?:.

var place = connections == "Connections" ? monsterQuery1 : monsterQuery2;

I do not think this is a good solution because your queries are too big (unreadable).

It would be much better if you introduced a named class that you use instead of the anonymous type. R# does that for you in a "light bulb menu" refactoring.

Upvotes: 2

Related Questions