Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring anonymous type as array correctly to keep scope

Tags:

c#

linq

c#-4.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)
            ....
like image 962
Sealer_05 Avatar asked Jul 15 '26 10:07

Sealer_05


1 Answers

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.

like image 199
Jon Skeet Avatar answered Jul 17 '26 16:07

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!