我试着在LINQ中将下面的SQL查询写到实体中,我该怎么做呢?
SELECT codigo FROM Vendas ORDER BY codigo DESC LIMIT 1;
我知道如何使用
DESC
,但是LIMIT 1
呢。我可以用我所知道的东西来编写这种代码,但如果表上有多行,显然会抛出异常:using (TccContext db = new TccContext())
{
var query = (from venda in db.Vendas
orderby venda.codigo descending // `LIMIT 1` in LINQ should be added here I think
select venda).SingleOrDefault();
}
提前谢谢。
最佳答案
using (TccContext db = new TccContext())
{
var query = (from venda in db.Vendas
orderby venda.codigo descending
select venda).FirstOrDefault();
}
如果需要一个元素的可枚举性(实际上类似于SQL
limit
):using (TccContext db = new TccContext())
{
var query = (from venda in db.Vendas
orderby venda.codigo descending
select venda).Take(1);
}