修改linq以获得前5个元素

use*_*207 1 c# linq

var lastArticles = from a in be.MyTable
                   where a.id == 1
                   join c in be.OtherTable on a.parent equals c.id
                   orderby a.timestamp descending
                   select new { a, cName = c.name};
Run Code Online (Sandbox Code Playgroud)

我需要获得前5个元素.

我是这样做的

.Take(5)
Run Code Online (Sandbox Code Playgroud)

但在linq声明中有没有办法呢?

Yuc*_*uck 6

不,您需要使用Skip()Take()作为方法调用.没有LINQ特定的等价物.

var lastArticles = (from a in be.MyTable
                    where a.id == 1
                    join c in be.OtherTable on a.parent equals c.id
                    orderby a.timestamp descending
                    select new { a, cName = c.name }).Take(5);
Run Code Online (Sandbox Code Playgroud)