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

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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

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

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

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

Any ideas what would cause this?

回答1:

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

using System.Linq;

This fixed it right up.



回答2:

In my case it was missing

using System.Data.Entity;



回答3:

In my case, I had the

Using System.Linq;

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

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.



回答4:

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

columns.Bound(x => x.ID).Template(@@item.ID);

was supposed to be this:

columns.Bound(x => x.Id).Template(@@item.Id);

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!



回答5:

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


回答6:

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 ()!!!



回答7:

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

List lst = db.Taksit.Where(y => y.OgrenciId.Equals(Convert.ToInt32( list[0].        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!