实体框架-字符串排序导致“等待操作超时”
本文关键字:等待 操作 超时 框架 字符串 排序 实体 | 更新日期: 2023-09-27 18:11:11
当我使用Name
排序时,我有以下查询导致"等待操作超时"。我怎样才能使它更快呢?Foods
表中有8.5k行。
Expression<Func<Food, string>> byName = x => x.FoodTranslations.FirstOrDefault(y => y.Language.Code == CultureInfo.CurrentUICulture.Name).Name;
Expression<Func<Food, string>> byUdsa = x => x.Udsa;
var query = this.DbContext.Foods
.Where(x => category == null || x.FoodTypeId == category)
.Where(x => (string.IsNullOrEmpty(name) || x.FoodTranslations.FirstOrDefault(y=>CultureInfo.CurrentUICulture.Name == y.Language.Code).Name.Contains(name)));
switch (order)
{
case 1:
query = query.OrderBy(byUdsa);
break;
default:
query = query.OrderBy(byName);
break;
}
var result = new JsonFoodIndexResult();
result.Foods = query.Skip(start)
.Take(size)
.Select(x => new JsonFoodIndex
{
Name = x.FoodTranslations.FirstOrDefault(y => y.Language.Code == CultureInfo.CurrentUICulture.Name).Name,
Id = x.Id,
Udsa = x.Udsa,
Category = x.FoodType.FoodTypeTranslations.FirstOrDefault(y => y.Language.Code == CultureInfo.CurrentUICulture.Name).Name
}).ToList();
try
query = query.AsNoTracking().OrderBy(byName);
实体框架创建代理对象来跟踪每条被检索到的记录的变化&这个创作花了很多时间。由于在检索时不需要跟踪,因此可以如上所示将其设置为Off。