无法将 lambda 表达式转换为“字符串”类型;因为它不是委托类型

发布于 2024-10-27 21:32:20 字数 2215 浏览 1 评论 0原文

我正在构建一个页面,将 LINQ 查询结果显示为表格。

  1. 在“SetupArticleQuery()”方法中设置基本查询,该方法将查询保存到“this.articles”。
  2. 运行另一个方法“UpdateFilter()”对保存在“this.articles”中的结果进行一些过滤。

我收到错误

无法将 lambda 表达式转换为“string”类型,因为它不是委托类型

代码所在行的

this.articles = from art in this.articles
                where art.category_id == this.categoryId
                select art;

委托类型有什么想法如何修复下面的代码吗?

namespace WebApplication3 {
    public partial class _Default : System.Web.UI.Page {
        private IQueryable articles;

        protected void Page_Load(object sender, EventArgs e) {
            this.SetupArticleQuery();
            this.UpdateFilter();
        }

        private void SetupArticleQuery() {
            this.articles = from a in KB.Articles
                            join t in KB.Teams on a.primary_team_id equals t.id
                            join cat in KB.Categories on a.category_id equals cat.id
                            join scat in KB.SubCategories on a.subcategory_id equals scat.id
                            join top in KB.Topics on a.topic_id equals top.id
                            select new {
                                a.id,
                                a.title,
                                a.view_count,
                                a.created_at,
                                a.created_by,
                                a.primary_team_id,
                                primary_team_name = t.name,
                                category_id = cat.id,
                                category_name = cat.name,
                                subcategory_id = scat.id,
                                subcategory_name = scat.name,
                                topic_id = top.id,
                                topic_name = top.name
                            };
        }

        private void UpdateFilter() {
            if (this.categoryId > 0) {
                this.articles = from art in this.articles
                                where art.category_id == this.categoryId
                                select art;

            }
        }
}

I'm building a page that shows a LINQ query result as a table.

  1. Setup the base query in the 'SetupArticleQuery()' method which saves the query to 'this.articles'.
  2. Run another method, 'UpdateFilter()' to do some filtering on the results that are saved in 'this.articles'.

I get the error

Cannot convert lambda expression to type 'string' because it is not a delegate type

at the line with the code

this.articles = from art in this.articles
                where art.category_id == this.categoryId
                select art;

Any ideas how to fix the code below?

namespace WebApplication3 {
    public partial class _Default : System.Web.UI.Page {
        private IQueryable articles;

        protected void Page_Load(object sender, EventArgs e) {
            this.SetupArticleQuery();
            this.UpdateFilter();
        }

        private void SetupArticleQuery() {
            this.articles = from a in KB.Articles
                            join t in KB.Teams on a.primary_team_id equals t.id
                            join cat in KB.Categories on a.category_id equals cat.id
                            join scat in KB.SubCategories on a.subcategory_id equals scat.id
                            join top in KB.Topics on a.topic_id equals top.id
                            select new {
                                a.id,
                                a.title,
                                a.view_count,
                                a.created_at,
                                a.created_by,
                                a.primary_team_id,
                                primary_team_name = t.name,
                                category_id = cat.id,
                                category_name = cat.name,
                                subcategory_id = scat.id,
                                subcategory_name = scat.name,
                                topic_id = top.id,
                                topic_name = top.name
                            };
        }

        private void UpdateFilter() {
            if (this.categoryId > 0) {
                this.articles = from art in this.articles
                                where art.category_id == this.categoryId
                                select art;

            }
        }
}

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

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

发布评论

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

评论(4

迟月 2024-11-03 21:32:20

我必须添加以下内容才能消除此错误。

using System.Data;
using System.Data.Entity;

I had to add the following to get this error to go away.

using System.Data;
using System.Data.Entity;
娜些时光,永不杰束 2024-11-03 21:32:20

实际上我没有发现你的代码有任何问题。

从这个答案< /a>,我建议确认您已添加:

Using System.Linq;

祝您好运!

Actually I haven't found any problem in your code.

From this answer, I would suggest to confirm that you have added:

Using System.Linq;

Good luck!

轻拂→两袖风尘 2024-11-03 21:32:20

此查询:

this.articles = from a in KB.Articles
                join t in KB.Teams on a.primary_team_id equals t.id
                join cat in KB.Categories on a.category_id equals cat.id
                join scat in KB.SubCategories on a.subcategory_id equals scat.id
                join top in KB.Topics on a.topic_id equals top.id
                select new {
                    a.id,
                    a.title,
                    a.view_count,
                    a.created_at,
                    a.created_by,
                    a.primary_team_id,
                    primary_team_name = t.name,
                    category_id = cat.id,
                    category_name = cat.name,
                    subcategory_id = scat.id,
                    subcategory_name = scat.name,
                    topic_id = top.id,
                    topic_name = top.name
                };

不会像这样工作,因为它返回匿名类型。因此,您必须将其键入为 var,这对于类级别成员无效;只能使用 var 作为局部变量。

您需要做的是创建一个实际的类来保存您的投影,并具有类似以下内容:

...
join top in KB.Topics on a.topic_id equals top.id
select new LocalDTO()
{
    id = a.id,
    ...
};

从那里您可以只具有:

private void UpdateFilter()
{
    if (this.categoryId > 0)
        this.articles = this.articles.Where(a => art.category_id);
}

当然 this.articles 将被声明为 IQueryable

This query:

this.articles = from a in KB.Articles
                join t in KB.Teams on a.primary_team_id equals t.id
                join cat in KB.Categories on a.category_id equals cat.id
                join scat in KB.SubCategories on a.subcategory_id equals scat.id
                join top in KB.Topics on a.topic_id equals top.id
                select new {
                    a.id,
                    a.title,
                    a.view_count,
                    a.created_at,
                    a.created_by,
                    a.primary_team_id,
                    primary_team_name = t.name,
                    category_id = cat.id,
                    category_name = cat.name,
                    subcategory_id = scat.id,
                    subcategory_name = scat.name,
                    topic_id = top.id,
                    topic_name = top.name
                };

Won't work like this since it returns an anonymous type. As a result, you'd have to type it as var, which is invalid for class-level members; you can only use var for local variables.

What you need to do is create an actual class to hold your projection, and have something like:

...
join top in KB.Topics on a.topic_id equals top.id
select new LocalDTO()
{
    id = a.id,
    ...
};

From there you could just have:

private void UpdateFilter()
{
    if (this.categoryId > 0)
        this.articles = this.articles.Where(a => art.category_id);
}

And of course this.articles would be declares as IQueryable<LocalDTO>.

や三分注定 2024-11-03 21:32:20

如果是匿名类型的问题,可以这样做吗?

private void UpdateFilter() {
    if (this.categoryId > 0) {
        this.articles = this.articles.Where(a => a.category_id == this.categoryId).AsQueryable();
    }
}

这可能是错误的,因为底层的 lambda 是相同的。

If it is a problem with anonymous types, can you do something like this?

private void UpdateFilter() {
    if (this.categoryId > 0) {
        this.articles = this.articles.Where(a => a.category_id == this.categoryId).AsQueryable();
    }
}

This may be off base, since the underlying lambda is the same.

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