2010-02-08 2 views
1

Я действительно озадачен этим. Я пытаюсь передать мой сообщений vaiable к методу. Как идти об этом: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()', если ваша объектная модель настроена правильно. –

ответ

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.