使用实体框架6.0.2和.NET 4.5.1在的Visual Studio 2013更新1与连接到SQL Server的一个DbContext
:“跳过”方法仅支持LINQ to Entities中的排序输入。该方法 '排序依据' 必须调用该方法之前, '跳过'
我有一个很长的过滤器链,我正在应用到基于调用者的期望结果的查询。一切都很好,直到我需要添加分页。这里是一个一瞥:
IQueryable<ProviderWithDistance> results = (from pl in db.ProviderLocations
let distance = pl.Location.Geocode.Distance(_geo)
where pl.Location.Geocode.IsEmpty == false
where distance <= radius * 1609.344
orderby distance
select new ProviderWithDistance() { Provider = pl.Provider, Distance = Math.Round((double)(distance/1609.344), 1) }).Distinct();
if (gender != null)
{
results = results.Where(p => p.Provider.Gender == (gender.ToUpper() == "M" ? Gender.Male : Gender.Female));
}
if (type != null)
{
int providerType;
if (int.TryParse(type, out providerType))
results = results.Where(p => p.Provider.ProviderType.Id == providerType);
}
if (newpatients != null && newpatients == true)
{
results = results.Where(p => p.Provider.ProviderLocations.Any(pl => pl.AcceptingNewPatients == null || pl.AcceptingNewPatients == AcceptingNewPatients.Yes));
}
if (string.IsNullOrEmpty(specialties) == false)
{
List<int> _ids = specialties.Split(',').Select(int.Parse).ToList();
results = results.Where(p => p.Provider.Specialties.Any(x => _ids.Contains(x.Id)));
}
if (string.IsNullOrEmpty(degrees) == false)
{
List<int> _ids = specialties.Split(',').Select(int.Parse).ToList();
results = results.Where(p => p.Provider.Degrees.Any(x => _ids.Contains(x.Id)));
}
if (string.IsNullOrEmpty(languages) == false)
{
List<int> _ids = specialties.Split(',').Select(int.Parse).ToList();
results = results.Where(p => p.Provider.Languages.Any(x => _ids.Contains(x.Id)));
}
if (string.IsNullOrEmpty(keyword) == false)
{
results = results.Where(p =>
(p.Provider.FirstName + " " + p.Provider.LastName).Contains(keyword));
}
这是我加入的底部(skip
和max
只是INT参数)分页:
if (skip > 0)
results = results.Skip(skip);
results = results.Take(max);
return new ProviderWithDistanceDto { Locations = results.AsEnumerable() };
现在我的问题(S):
正如你所看到的,我在最初的LINQ查询中做了order by,所以wh是否它抱怨我需要做
OrderBy
之前做一个Skip
(我以为我是?)...我假设它不会变成一个SQL查询并执行,直到我枚举结果,这就是为什么我等到最后一行返回结果AsEnumerable()。这是正确的方法吗?
如果我必须在做
Skip
和Take
之前列举结果,这将如何影响性能?很显然,我想让SQL Server完成繁重的工作,并只返回请求的结果。或者没有关系(或者我错了)?
快速注释:如果您使用的是“开关”语句来处理你的中将sortOrder,请尝试使用“默认”的情况下,将责令你的结果由您选择的字段设置。 – CodeBreaker