正确地将匿名类型声明为数组以保持范围

时间:2013-01-15 23:11:30

标签: c# linq c#-4.0

我想要做的就是正确地声明var place,这样一旦我进入foreach循环,它仍然在范围内。我假设我需要在if connections语句之前声明它。这是一个正确的假设,如果是这样,我该如何申报?谢谢!

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

3 个答案:

答案 0 :(得分:7)

您可以创建一个包含单个条目的数组,该条目的值稍后会被忽略:

// 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 = ...;
}

这是有效的,因为使用具有相同名称和类型的属性的匿名类型,使用相同的顺序,将使用相同的具体类型。

我认为最好只使代码 在所需的部分中有所不同:

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();

这里更容易告诉依赖于connections值的部分是处理WD_Connect的查询部分。

答案 1 :(得分:2)

您可以将if转换为?:

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

我不认为这是一个很好的解决方案,因为您的查询太大(不可读)。

如果您使用的是命名类而不是匿名类型,那会好得多。 R#在“灯泡菜单”重构中为你做到了这一点。

答案 2 :(得分:1)

你可以使用1个查询,因为它们几乎相同,只需在where子句中添加额外条件

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