从LINQ查询返回对象而不将其强制转换为列表

Lan*_*ins 1 c# linq asp.net

当我将返回值设置为a时List,我有一个LINQ查询,但是我只想返回一个IQueryable<Post>.我该怎么办?

public List<Post> GetPostByID(int id)
{
    var thePost = (from p in _context.Posts
                   where p.Id == id
                   select p).ToList();
    return thePost;
}
Run Code Online (Sandbox Code Playgroud)

Ada*_*Dev 8

 public IQueryable<Post> GetPostByID(int id)
 {
        return (from p in _context.Posts
                       where p.Id == id
                       select p);
 }
Run Code Online (Sandbox Code Playgroud)

当然,由于延迟执行,此查询将在调用者尝试枚举结果的put处执行.