Linq to Entities:如何将IQueryable传递给Method

时间:2010-02-08 19:43:30

标签: entity-framework linq-to-entities iqueryable

我对这个感到非常困惑。我正在尝试将帖子 vaiable传递给方法。我该如何做到这一点:

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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()
        };

从这里开始,您可以使用@Lahmann的回答和强类型IQueryable。