2010-02-08 1 views
1

Je suis vraiment déconcerté sur celui-ci. J'essaie de passer mes postes vaiable à une méthode. Comment puis-je à faire ceci:Linq to Entities: Comment puis-je passer un IQueryable à une méthode

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

La requête Linq pourrait probablement être simplifiée quelque peu. Par exemple, 'FavoriteCount = p.post.favorites.count()' si votre modèle d'objet est configuré correctement. –

Répondre

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

Vous ne pouvez pas le faire avec des objets anonymes (le select new { }). Vous devrez spécifier un type fort là-dedans, comme:

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

A partir de là, vous pouvez utiliser @ la réponse de Luhmann et IQueryable fortement type.