1

我试图在 LINQ to Entities 中编写以下 SQL 查询,我该怎么做?

SELECT codigo FROM Vendas ORDER BY codigo DESC LIMIT 1;

我知道如何使用DESC,但是LIMIT 1. 我可以用我知道的东西编写这种和平的代码,但是如果 table 上有不止一行,它显然会抛出异常Vendas

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();
}

提前致谢。

4

1 回答 1

3
using (TccContext db = new TccContext())
{
    var query = (from venda in db.Vendas
                 orderby venda.codigo descending
                 select venda).FirstOrDefault();
}

如果您需要 Enumerable 与一个元素(实际上是 SQL 的模拟limit):

using (TccContext db = new TccContext())
{
    var query = (from venda in db.Vendas
                 orderby venda.codigo descending
                 select venda).Take(1);
}
于 2013-09-01T19:59:15.367 回答