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

Dam*_*iro 8 .net c# linq lambda extension-methods

我正在构建一个页面,将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;
Run Code Online (Sandbox Code Playgroud)

任何想法如何修复下面的代码?

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;

            }
        }
}
Run Code Online (Sandbox Code Playgroud)

小智 28

我不得不添加以下内容以使此错误消失.

using System.Data;
using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)


Hom*_*mam 13

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

从这个答案,我建议你确认你已经添加:

Using System.Linq;
Run Code Online (Sandbox Code Playgroud)

祝好运!


Ada*_*kis 5

这个查询:

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
                };
Run Code Online (Sandbox Code Playgroud)

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

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

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

从那里你可以得到:

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

当然,this.articles将被声明为IQueryable<LocalDTO>.