【发布时间】:2011-03-31 14:42:54
【问题描述】:
我正在构建一个将 LINQ 查询结果显示为表格的页面。
- 在“SetupArticleQuery()”方法中设置基本查询,将查询保存到“this.articles”。
- 运行另一个方法“UpdateFilter()”对保存在“this.articles”中的结果进行一些过滤。
我得到了错误
无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
在代码行
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;
}
}
}
【问题讨论】:
-
我遇到了同样的问题,因为我只是错过了使用 System.Linq;
标签: c# .net linq lambda extension-methods