我想要做的就是正确声明var place
,所以一旦我到达foreach
循环,它仍然在范围内。我假设我需要在connections
的if
声明之前声明它。这是一个正确的假设,如果是的话,我该如何申报?谢谢!正确声明匿名类型为数组以正确保持作用域
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)
....
你可以在'if'折叠成'where',而是和只有一个查询?我不认为你可以在分配给它之前声明一个匿名类型的变量。 – Blorgbeard
@Blorgbeard可能会阻止SQL Server使用索引。取决于OPs模式。 – usr
@usr我正在使用索引。感谢您注意 –