Is it possible to write IQueryable<MyObject> = query.Take(1)
or something equivalent in LINQ query syntax. I'm using C# 5 and EF 5.
Asked
Active
Viewed 1.1k times
26

Steven Wexler
- 16,589
- 8
- 53
- 80
-
1Unfortunately no, C# doesn't have as many in-language Linq keywords as VB, but you can do it without it looking too messy by adding `.Skip(n).Take(m)` at the end. – Dai Jul 26 '13 at 20:46
-
@TimSchmelter oh cool. Out of curiosity, what is the syntax in VB.NET? – Steven Wexler Jul 26 '13 at 20:47
-
`Take` realized the query, it should be keep separate from the logical query. – asawyer Jul 26 '13 at 20:47
-
2@Steaks: `From r In query Take 2` (skip works similar) – Tim Schmelter Jul 26 '13 at 20:47
-
In addition to Take, VB also adds Distinct, Aggregate, Count, Sum as keywords. – Jim Wooley Jul 26 '13 at 21:06
-
[Query keywords](https://msdn.microsoft.com/en-us/library/bb310804.aspx) - this might be helpfull – Bakudan Jul 07 '16 at 13:32
2 Answers
28
There is no equivalent to Take
in the query expression syntax for LINQ in C#. The only methods that have query expression equivalents are
Where,
Select,
SelectMany,
Join,
GroupJoin,
OrderBy,
OrderByDescending,
ThenBy,
ThenByDescending,
GroupBy,
Cast
This is from §7.16.2 of the specification.

jason
- 236,483
- 35
- 423
- 525
23
No. You have to use the dot syntax for that operation. Same goes for ToList
, Count
, etc...
var query =
(from item in list
where predicate(item)
select func(item))
.Take(10);

Timothy Shields
- 75,459
- 18
- 120
- 173