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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2058487/
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-06 23:07:55  来源:igfitidea点击:

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

c#.netentity-frameworklambda

提问by Keith Barrows

I am using Entity Framework in my C# based code. I am running into an unexpected weirdness and am looking for suggestions.

我在基于 C# 的代码中使用实体框架。我遇到了意想不到的怪事,正在寻找建议。

Case 1, 2, 3, 4... Projects:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

案例 1、2、3、4... 项目:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

Samples (all of these work):
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

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();
}

etc

等等

Case "weirdness":
RivWorks.Web.Service.dll (WCF project)
Contains the same references as the other projects.

案例“怪异”:
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();
    ...
}

I am getting this compile time error (the word "where" is highlighted in my editor):
Cannot convert lambda expression to type 'string' because it is not a delegate type

我收到此编译时错误(“where”一词在我的编辑器中突出显示):
无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型

Any ideas what would cause this?

任何想法会导致这种情况?

采纳答案by Keith Barrows

For those interested in the outcome:
I was missing a simple Using statement at the head of my code.

对于那些对结果感兴趣的人:
我在代码的开头缺少一个简单的 Using 语句。

using System.Linq;

This fixed it right up.

这修复了它。

回答by jhamm

In my case, I had the

就我而言,我有

Using System.Linq;

but I was missing the && after a where clause item.

但我在 where 子句项之后错过了 && 。

Bad Code:

错误代码:

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

Correct Code ( with no error ):

正确代码(没有错误):

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

I hope this saves someone some time.

我希望这可以节省一些时间。

回答by Chris

I was struggling with this in a Telerik Grid template within a Razor view for about an hour. In my case, this:

我在 Razor 视图中的 Telerik Grid 模板中为此苦苦挣扎了大约一个小时。就我而言,这是:

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

was supposed to be this:

应该是这样的:

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

The case on "Id" was wrong! I hope this helps someone. You might be getting this error just because you put a non-existent property!

关于“Id”的案例是错误的!我希望这可以帮助别人。您可能会收到此错误,因为您放置了一个不存在的属性!

回答by Craig Fisher

I was having a similar problem binding columns to a Telerik MVC Grid. I had an Id property on my ViewModel class. (Inspired by Chris's answer above) I renamed it to XxxId and the problem went away. I seem to recall something about MVC doing something special for Id properties.

我在将列绑定到 Telerik MVC 网格时遇到了类似的问题。我的 ViewModel 类有一个 Id 属性。(受 Chris 上面的回答启发)我将其重命名为 XxxId,问题就消失了。我似乎想起了 MVC 为 Id 属性做一些特别的事情。

回答by Valentin Kuzub

I had a similar looking problem but with Rx and in my case adding

我有一个类似的问题,但与 Rx 并在我的情况下添加

using System;

helped. FWIW

有帮助。FWIW

Mess with those extension methods missing is too annoying sometimes.

有时,缺少那些扩展方法太烦人了。

回答by mathel

i had the same problem with mvc 3 razor maybe someone has the same so i wanna show how to fix it in my stuation

我对 mvc 3 razor 有同样的问题,也许有人有同样的问题,所以我想展示如何在我的情况下修复它

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

i tried to use contains but hence OgrenciId is int i get the error mentioned here so by using equals the problem is solved

我试图使用 contains 但因此 OgrenciId 是 int 我得到这里提到的错误所以通过使用 equals 问题解决了

回答by FastDev

I stumbled upon this and found a different fix. I was using var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong");and getting the mentioned error. The mistake was I was using the method FullNameReverse()as property FullNameReverse. Missed the ()!!!

我偶然发现了这一点,并找到了不同的解决方法。我正在使用 var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong");并收到提到的错误。错误是我将该方法FullNameReverse()用作 property FullNameReverse。错过了()!!!

回答by Miguel Galante

In my case it was missing

就我而言,它不见了

using System.Data.Entity;

using System.Data.Entity;

回答by Demelziraptor

In my case I had this error when trying to use Include in clientContext.Load in a Sharepoint 2013 app.

在我的情况下,我在 Sharepoint 2013 应用程序中尝试在 clientContext.Load 中使用 Include 时遇到此错误。

I had included Sharepoint client library like so:

我已经包含了 Sharepoint 客户端库,如下所示:

using SP = Microsoft.SharePoint.Client;

To fix, in addition I also added it without the namespacing:

为了修复,另外我还添加了它而不带命名空间:

using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

回答by vapcguy

My issue involved the format:

我的问题涉及格式:

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

Every p.Whatever had this error on it.

每个 p.Whatever 上都有这个错误。

This was a project using MVC, and I found my issue was having "public static int" or "public static string" on these variables (Id, Name, etc.) in my model. When I removed "static" on all my model variables, I no longer got the error. Was driving me nuts for about a day...

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