您如何查询搜索结果中的块?
How do you query for a block inside of search results?
假设你有这个 table:
var query = db.Cars.OrderBy(u => u.Name);
ID Name
17 Accord
772 Civic
36 F-150
2237 Model 3
425 Sienna
ID=772 之后的两行是按名称排序的,如何抓取?这些是期望的结果:
ID Name
36 F-150
2237 Model 3
425 Sienna
看看SkipWhile
方法
var query = db.Cars.OrderBy(u => u.Name)
.SkipWhile(u => u.Id != 772)
.Skip(1)
.Take(2);
如果您的要求是:
Give me all cars with a Name alphabetically higher than "Civic"
(或高于某些输入变量)
return db.Cars.Where(car.Name > "Civic")
.OrderBy(car.Name);
显然,您的要求很不寻常。
Given some input car id (772), give me all Cars with a name alphabetically higher than the name the car with this id.
因为访问数据库,所以不能使用 SkipWhile,它不支持 IQueryable。
string nameOfCar772 = db.Cars.Where(car => car.Id == 772)
.Select(car => car.Name)
.FirstOrDefault();
// TODO: handle if there is no car with id 772, so if car772 == null
return db.Cars.Where(car => car.Name > nameOfCar772)
.OrderBy(car => car.Name);
唉,这需要两次数据库访问。
假设你有这个 table:
var query = db.Cars.OrderBy(u => u.Name);
ID Name
17 Accord
772 Civic
36 F-150
2237 Model 3
425 Sienna
ID=772 之后的两行是按名称排序的,如何抓取?这些是期望的结果:
ID Name
36 F-150
2237 Model 3
425 Sienna
看看SkipWhile
方法
var query = db.Cars.OrderBy(u => u.Name)
.SkipWhile(u => u.Id != 772)
.Skip(1)
.Take(2);
如果您的要求是:
Give me all cars with a Name alphabetically higher than "Civic"
(或高于某些输入变量)
return db.Cars.Where(car.Name > "Civic")
.OrderBy(car.Name);
显然,您的要求很不寻常。
Given some input car id (772), give me all Cars with a name alphabetically higher than the name the car with this id.
因为访问数据库,所以不能使用 SkipWhile,它不支持 IQueryable。
string nameOfCar772 = db.Cars.Where(car => car.Id == 772)
.Select(car => car.Name)
.FirstOrDefault();
// TODO: handle if there is no car with id 772, so if car772 == null
return db.Cars.Where(car => car.Name > nameOfCar772)
.OrderBy(car => car.Name);
唉,这需要两次数据库访问。