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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19197481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:22:27  来源:igfitidea点击:

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

c#asp.netlinqlambda

提问by Deep Sharma

I am using a LINQ lambda expression like so:

我正在使用 LINQ lambda 表达式,如下所示:

int Value = 1;
qryContent objContentLine;

using (Entities db = new Entities())
{
    objContentLine = (from q in db.qryContents
                      where q.LineID == Value
                      orderby q.RowID descending
                      select q).FirstOrDefault();
}

However, I am getting the following error:

但是,我收到以下错误:

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

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

采纳答案by S. S. Rawat

I think you are missing using System.Linq;from this system class.

我认为您错过using System.Linq;了这个系统课程。

and also add using System.Data.Entity;to the code

并添加using System.Data.Entity;到代码中

回答by skb

In my case, I had to add using System.Data.Entity;

就我而言,我不得不添加 using System.Data.Entity;

回答by Matthew

For people just stumbling upon this now, I resolved an error of this type that was thrown with all the references and using statements placed properly. There's evidently some confusion with substituting in a function that returns DataTable instead of calling it on a declared DataTable. For example:

对于现在刚刚遇到这个问题的人,我解决了这种类型的错误,该错误在所有引用和 using 语句放置正确时引发。显然,在一个返回 DataTable 的函数中进行替换,而不是在声明的 DataTable 上调用它,这显然有些混淆。例如:

This worked for me:

这对我有用:

DataTable dt = SomeObject.ReturnsDataTable();

List<string> ls = dt.AsEnumerable().Select(dr => dr["name"].ToString()).ToList<string>();

But this didn't:

但这并没有:

List<string> ls = SomeObject.ReturnsDataTable().AsEnumerable().Select(dr => dr["name"].ToString()).ToList<string>();

I'm still not 100% sure why, but if anyone is frustrated by an error of this type, give this a try.

我仍然不能 100% 确定原因,但是如果有人对此类错误感到沮丧,请尝试一下。

回答by user4238584

My case it solved i was using

我的情况它解决了我正在使用

@Html.DropDownList(model => model.TypeId ...)  

using

使用

@Html.DropDownListFor(model => model.TypeId ...) 

will solve it

会解决它

回答by AFract

If it's not related to missing using directives stated by other users, this will also happen if there is another problem with your query.

如果它与缺少其他用户声明的 using 指令无关,那么如果您的查询存在另一个问题,也会发生这种情况。

Take a look on VS compiler error list : For example, if the "Value" variable in your query doesn't exist, you will have the "lambda to string" error, and a few errors after another one more related to the unknown/erroneous field.

查看VS编译器错误列表:例如,如果查询中的“Value”变量不存在,则会出现“lambda to string”错误,并且一个接一个的错误与未知/错误的领域。

In your case it could be :

在你的情况下,它可能是:

objContentLine = (from q in db.qryContents
                  where q.LineID == Value
                  orderby q.RowID descending
                  select q).FirstOrDefault();

Errors:

错误:

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

Error 242 Delegate 'System.Func<..>' does not take 1 arguments

Error 243 The name 'Value' does not exist in the current context

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

错误 242 委托 'System.Func<..>' 不接受 1 个参数

错误 243 当前上下文中不存在名称“值”

Fix the "Value" variable error and the other errors will also disappear.

修复“Value”变量错误,其他错误也会消失。