This question already has an answer here:
这个问题已经有了答案:
- Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type 13 answers
- 实体框架-不能将lambda表达式转换为类型“string”,因为它不是一个委托类型13的答案。
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表达式转换为类型“string”,因为它不是一个委托类型。
5 个解决方案
#1
184
I think you are missing using System.Linq;
from this system class.
我认为你是在使用System.Linq;从这个系统类。
and also add using System.Data.Entity;
to the code
并添加使用System.Data.Entity;代码
#2
91
In my case, I had to add using System.Data.Entity;
在我的例子中,我必须添加使用System.Data.Entity;
#3
10
My case it solved i was using
我的案子解决了我的问题。
@Html.DropDownList(model => model.TypeId ...)
using
使用
@Html.DropDownListFor(model => model.TypeId ...)
will solve it
将解决这个问题
#4
4
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.
如果您的查询还有其他问题,如果没有使用其他用户声明的指令,这也会发生。
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编译器错误列表:例如,如果您的查询中“值”变量不存在,那么您将会有“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
错误241不能将lambda表达式转换为类型“string”,因为它不是一个委托类型。
Error 242 Delegate 'System.Func<..>' does not take 1 arguments
错误242代表“System.Func < . .>不接受1个参数。
Error 243 The name 'Value' does not exist in the current context
错误243当前上下文不存在名称“值”。
Fix the "Value" variable error and the other errors will also disappear.
修正“值”变量错误,其他错误也会消失。
#5
2
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:
对于刚刚遇到这种情况的人,我解决了这个类型的错误,该错误被抛出了所有的引用,并正确地使用了语句。显然,在一个返回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.
我仍然不能百分之百地确定为什么,但是如果有人因为这种类型的错误而感到沮丧,请尝试一下。
#1
184
I think you are missing using System.Linq;
from this system class.
我认为你是在使用System.Linq;从这个系统类。
and also add using System.Data.Entity;
to the code
并添加使用System.Data.Entity;代码
#2
91
In my case, I had to add using System.Data.Entity;
在我的例子中,我必须添加使用System.Data.Entity;
#3
10
My case it solved i was using
我的案子解决了我的问题。
@Html.DropDownList(model => model.TypeId ...)
using
使用
@Html.DropDownListFor(model => model.TypeId ...)
will solve it
将解决这个问题
#4
4
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.
如果您的查询还有其他问题,如果没有使用其他用户声明的指令,这也会发生。
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编译器错误列表:例如,如果您的查询中“值”变量不存在,那么您将会有“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
错误241不能将lambda表达式转换为类型“string”,因为它不是一个委托类型。
Error 242 Delegate 'System.Func<..>' does not take 1 arguments
错误242代表“System.Func < . .>不接受1个参数。
Error 243 The name 'Value' does not exist in the current context
错误243当前上下文不存在名称“值”。
Fix the "Value" variable error and the other errors will also disappear.
修正“值”变量错误,其他错误也会消失。
#5
2
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:
对于刚刚遇到这种情况的人,我解决了这个类型的错误,该错误被抛出了所有的引用,并正确地使用了语句。显然,在一个返回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.
我仍然不能百分之百地确定为什么,但是如果有人因为这种类型的错误而感到沮丧,请尝试一下。