How can I write Take(1) in query syntax

孤者浪人 提交于 2019-11-30 07:54:37

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/17890729/how-can-i-write-take1-in-query-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!