WEB API IQueryable skip()take() [英] WEB API IQueryable skip() take()
问题描述
所以我想在C#ASP.NET Web Api中切片一个"IQueryable",但是我无法使其工作.使用下面的代码,它可能会更清楚我想要做什么.
So i want to slice a "IQueryable" in C# ASP.NET Web Api but i can't make it work. With the code below it might get more clear what i want to do.
private TestTwoContext db = new TestTwoContext();
// GET: api/Fruits/thefruits
[HttpGet]
[Route("api/Fruits/thefruits")]
public IQueryable<Fruits> TheFruits()
{
dynamic myList = db.Fruits;
var FiveItems = Queryable.Take(Queryable.Skip(myList, 5), 5);
return FiveItems;
}
这在浏览器控制台中给我错误:
this gives me error in browser console:
jquery-3.3.1.min.js:2 GET http://localhost:49771/api/Fruits/thefruits 500(内部服务器错误)
jquery-3.3.1.min.js:2 GET http://localhost:49771/api/Fruits/thefruits 500 (Internal Server Error)
我需要将列表(db.Fruits)从第5个元素剪切到第10个元素. 就像我们在JavaScript中切片数组一样. 示例:
i need to cut my list (db.Fruits) from the 5th element to the 10th element. Just like we slice arrays in JavaScript. Example:
var myList = array.slice(5, 10)
我也尝试过这个:
private TestTwoContext db = new TestTwoContext();
// GET: api/Fruits/thefruits
[HttpGet]
[Route("api/Fruits/thefruits")]
public IQueryable<Fruits> TheFruits()
{
var myList = db.Fruits;
var FiveItems = myList.Skip(5).Take(4);
return FiveItems;
}
这给我浏览器控制台中的错误: jquery-3.3.1.min.js:2 GET http://localhost:49771/api/Fruits/thefruits 500(内部服务器错误)
this gives me error in browser console: jquery-3.3.1.min.js:2 GET http://localhost:49771/api/Fruits/thefruits 500 (Internal Server Error)
跳过和带走中的数字将作为参量传递,但这不是我担心的部分...这些数字现在仅是示例.任何帮助将不胜感激.
the number in skip and take will be passed as parametars but that is not that part i am worried about... those number are just example for now. Any help would be appreciated.
此控制器可以正常工作:
This Conroler works without problems:
// GET: api/Fruits
public IQueryable<Fruits> GetFruits()
{
return db.Fruits;
}
返回整个水果列表,稍后我将其记录在HTML表格中.
returns the whole list of fruits, which later i write it down in table in HTML.
这也有效:
[HttpGet]
[Route("api/Fruits/thefruits")]
public IQueryable<Fruits> TheFruits()
{
var myList = db.Fruits;
var FiveItems = myList.Take(5);
return FiveItems;
}
但是它给了我前5个元素...而不是第5个到第10个元素.
but it gives me the the first 5 elements... not from the 5th to 10th element.
推荐答案
如果您发布了500错误,那就太好了,但是作为一个猜测,这可能是因为Skip
如果不通过EntityFramework调用命令就无法实现
It would be nice if you posted the 500 error but as a guess, it's probably because cannot Skip
without calling an order by in EntityFramework
[HttpGet]
[Route("api/Fruits/thefruits")]
public IQueryable<Fruits> TheFruits()
{
return db.Fruits.OrderBy(x => x.Id).Skip(4).Take(5);
}
这篇关于WEB API IQueryable skip()take()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!