无法将 lambda 表达式转换为“字符串”类型;因为它不是委托类型
我正在构建一个页面,将 LINQ 查询结果显示为表格。
- 在“SetupArticleQuery()”方法中设置基本查询,该方法将查询保存到“this.articles”。
- 运行另一个方法“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.
- Setup the base query in the 'SetupArticleQuery()' method which saves the query to 'this.articles'.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我必须添加以下内容才能消除此错误。
I had to add the following to get this error to go away.
实际上我没有发现你的代码有任何问题。
从这个答案< /a>,我建议确认您已添加:
祝您好运!
Actually I haven't found any problem in your code.
From this answer, I would suggest to confirm that you have added:
Good luck!
此查询:
不会像这样工作,因为它返回匿名类型。因此,您必须将其键入为
var
,这对于类级别成员无效;只能使用 var 作为局部变量。您需要做的是创建一个实际的类来保存您的投影,并具有类似以下内容:
从那里您可以只具有:
当然 this.articles 将被声明为
IQueryable
。This query:
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:
From there you could just have:
And of course this.articles would be declares as
IQueryable<LocalDTO>
.如果是匿名类型的问题,可以这样做吗?
这可能是错误的,因为底层的 lambda 是相同的。
If it is a problem with anonymous types, can you do something like this?
This may be off base, since the underlying lambda is the same.