实体框架 - 无法将 lambda 表达式转换为类型 'string',因为它不是委托类型。

77

我在我的C#代码中使用Entity Framework。我遇到了一个意外的怪事,并正在寻求建议。

情况1、2、3、4......

项目:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

示例(所有这些都有效):
RivWorks.Alpha.dll:

public static bool EndNegotitation(long ProductID)
{
    var product = (from a in _dbFeed.AutoWithImage 
                   where a.AutoID == ProductID select a).FirstOrDefault();
...
}

RivWorks.Service.dll

public static RivWorks.Model.NegotiationAutos.AutoWithImage 
    GetProductById(long productId)
{
    var myProduct = from a in _dbFeed.AutoWithImage 
                    where a.AutoID == productId select a;

    return myProduct.FirstOrDefault();
}
public static List<RivWorks.Model.NegotiationAutos.AutoWithImage> 
    GetProductByCompany(Guid companyId)
{
    var myProduct = from a in _dbFeed.AutoWithImage 
                    where a.CompanyID == companyId select a;

    return myProduct.ToList();
}

情况“怪异”:
RivWorks.Web.Service.dll(WCF项目)
包含与其他项目相同的引用。

public NegotiateSetup GetSetup(string method, string jsonInput)
{
    ...
    long.TryParse(ProductID, out result);
    var product = (from a in _dbFeed.AutoWithImage 
                   where a.AutoID == result select a).FirstOrDefault();
    ...
}

我在编译时遇到了这个错误(编辑器中“where”一词被突出显示):
无法将 lambda 表达式转换为类型“string”,因为它不是委托类型

有任何想法是什么导致了这个错误吗?


这听起来很奇怪。如果你移除 FirstOrDefault 的调用,会发生什么?显然,在之后尝试使用 product 时它会失败,但是那个语句会编译通过吗? - Jon Skeet
另外,如果你将它改为 var product = _dbFeed.AutoWithImage.Where(a => a.AutoID == result);,那么会发生什么呢?让我们把查询表达式排除在外... - Jon Skeet
1
所有这些示例都失败了。然而,我检查了所有代码片段中的Using语句,并发现我缺少一个:Using System.Linq; 这解决了错误。<叹气/> - Keith Barrows
17个回答

116
对于那些对结果感兴趣的人:
我在代码头部漏掉了一个简单的Using语句。
using System.Linq;

这个修复了它。


19
我很惊讶,既然Visual Studio也好Resharper也好都没能够检测到这种情况。幸亏Stack Overflow比他们都要聪明。 - bwerks
2
我收到了相同的错误消息,并且我一直在使用System.LINQ(并且“始终如此”)。当然,模型成员之一将数据类型从string[]更改为string... - B. Clay Shannon-B. Crow Raven

106
在我的情况下,缺失的是using System.Data.Entity;

我有"using System.Data.EntityClient;",尽管它在那里,但是它是灰色的/不必要的。System.Data.Entity(没有"Client")是不允许的。 - B. Clay Shannon-B. Crow Raven
1
您,Miguel先生,是一个天才。谢谢! - oHoodie
真烦人。即使我重新启动了Visual Studio,智能感知也没有出现来解救。 - DankMemester 'Andrew Servania'

13
using System.Linq;
using System.Data.Entity;

7

我在 Razor 视图中的 Telerik Grid 模板中遇到了困难,持续了约一个小时。在我的情况下,是这样的:

columns.Bound(x => x.ID).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.ID)">@item.ID</a></text>);

应该是这样的:

columns.Bound(x => x.Id).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.Id)">@item.Id</a></text>);

“Id”案例是错误的!我希望这能帮助到某些人。您可能因为输入不存在的属性而出现此错误!


7
在我的情况下,我遇到了这个问题:
Using System.Linq;

但是我错过了where子句项目后的&&。

糟糕的代码:

item.Group.ID == grp.ID
p.Product_Status_Flag == 1 &&
item.Valid

正确代码(无误):

item.Group.ID == grp.ID && // <- This was missing and I didn't see it right away.
p.Product_Status_Flag == 1 &&
item.Valid

我希望这篇文章可以为某些人节省时间。

2

我偶然发现了这个问题,并找到了一个不同的解决方法。 我使用了 var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong"); 并遇到了上述错误。 错误在于我将方法 FullNameReverse() 作为属性 FullNameReverse 使用。 我忘记加 () 了!!!


1

这篇文章有点老了,但我刚遇到这个问题,在网上也找不到答案。有一个网站提到了导致答案的数据类型问题,但很可惜我再也找不到它了,所以我在这里发布我的解决方案。希望未来的搜索者能从中受益。

原始代码:IQueryable test = from r in Records where r.Record_ID == 100 select r;

其中Records是先前LINQ表达式产生的IQueryable。

解决方法是对Records进行转换:(IQueryable<record>)Records。一旦找到解决方法,就变得非常清晰。Records没有类型,因此LINQ不知道r.Record_ID是否有效。混乱之处在于错误消息,在几十个地方都出现在'net上,在几乎所有情况下,解决方案是缺少了一个或两个using语句。我找到的一个或两个不是using问题的例子,他们没有发布修改结果。

希望能有所帮助...


1

我在使用MVC 3 Razor时遇到了同样的问题,也许有人也遇到了这个问题,所以我想展示如何解决我的情况。

List<Taksit> lst = db.Taksit.Where(y => y.OgrenciId.Equals(Convert.ToInt32( list[0].Id))).ToList();

我尝试使用contains,但由于OgrenciId是int类型,所以出现了这里提到的错误,因此通过使用equals解决了问题。


0

我的问题涉及格式:

.Columns(columns => {
    columns.Bound(p => p.Id).Filterable(false);
    columns.Bound(p => p.Name).Width(250);
    ...

每个p.Whatever都出现了这个错误。

这是一个使用MVC的项目,我发现我的问题在于我的模型中的这些变量(Id,Name等)具有“public static int”或“public static string”。当我在所有模型变量上删除“static”时,我不再收到错误。这让我疯狂了一整天...


0
在我的情况下,当我尝试在Sharepoint 2013应用程序中使用clientContext.Load中的Include时,出现了这个错误。
我已经像这样包含了Sharepoint客户端库:
using SP = Microsoft.SharePoint.Client;

为了修复这个问题,我还添加了没有命名空间的代码:
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接