Linq to Entities:如何将 IQueryable 传递给方法

发布于 2024-08-21 12:06:50 字数 729 浏览 4 评论 0原文

我对这个真的很困惑。我正在尝试将我的帖子传递给一种方法。我该如何去做:

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

I am really baffled on this one. I am trying to pass my posts vaiable to a method. How do I go about doing this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-08-28 12:06:50
private static IQueryable<post> FilterQuestion(IQueryable<post> p, string sort)
private static IQueryable<post> FilterQuestion(IQueryable<post> p, string sort)
冬天的雪花 2024-08-28 12:06:50

您不能对匿名对象执行此操作(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()
        };

从这里,您可以使用@Luhmann 的答案和强类型 IQueryable。

You can't do this with anonymous objects (the select new { }). You'll have to specify a strong type in there, like:

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

From here, you can use @Luhmann's answer and strongly-type IQueryable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文