c# - 无法将 lambda 表达式转换为类型 'string',因为它不是委托(delegate)类型

标签 c# .net linq lambda extension-methods

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

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

我得到了错误

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

在代码所在的行

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;

            }
        }
}

最佳答案

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

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

关于c# - 无法将 lambda 表达式转换为类型 'string',因为它不是委托(delegate)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5501667/

相关文章:

c# - 从 LINQ to SQL 查询加载 DataGridView

linq - 在动态 Linq 中使用 "Single"

c# - 为什么我的隐私可以访问?

c# - EntityDataSource 查询内连接

c# - 如何确定并行 foreach 循环的性能是否优于 foreach 循环?

c# - 如何仅在整个文本被括号包围时才删除括号?

c# - 具有特定字边界的正则表达式

c# - 创建一个返回 Task<List<MyDataObject>> foo() 的方法

.net - VB.NET是否优化字符串文字的串联?

c# - 在 Linq 语句中无法识别对象,尽管在同一方法中可以访问该对象