【发布时间】:2013-01-15 23:11:30
【问题描述】:
我要做的就是正确声明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)
....
【问题讨论】:
-
您可以将
if折叠到where中,并且只有一个查询?我不认为你可以在分配给它之前声明一个匿名类型的变量。 -
@Blorgbeard 可能会阻止 SQL Server 使用索引。取决于 OP 架构。
-
@usr 我正在使用索引。感谢您注意到