2010-02-08 58 views
1

我对此非常困惑。我正尝试将的帖子变为可变的方法。我该如何去这样做:Linq to Entities:我如何将IQueryable传递给方法

var posts = from p in context.post 
      where (p.post_isdeleted == false && (object.Equals(p.post_parentid, null)) 
      select new 
      { 
       p.post_date, 
       p.post_id, 
       FavoriteCount = (from f in context.favorites 
           where f.post.post_id == p.post_id 
           select new { f }).Count() 
      }; 


posts = QuestionList.FilterQuestion(posts, sort); 

//the problem is here 
//IQueryable is not the right type for posts. What type do I put in there 
private static IQueryable FilterQuestion(IQueryable p, string sort) 
{ 
+0

Linq查询可能会有所简化。例如,如果你的对象模型设置正确,'FavoriteCount = p.post.favorites.count()'。 – 2010-02-08 19:58:21

回答

2
private static IQueryable<post> FilterQuestion(IQueryable<post> p, string sort) 
0

你不能用匿名对象(select new { })做到这一点。你必须在那里指定了一个强类型,如:

var posts = from p in context.post 
     where (p.post_isdeleted == false && (object.Equals(p.post_parentid, null)) 
     select new Post 
     { 
      Date = p.post_date, 
      Id = p.post_id, 
      FavoriteCount = (from f in context.favorites 
          where f.post.post_id == p.post_id 
          select new { f }).Count() 
     }; 

在这里,您可以用@卢曼的答案,强类型的IQueryable。